我正在编写一个使用组件的WPF应用程序,此组件返回一个指向位图像素(stride * height)的指针(IntPtr)。我预先知道位图是24位rgb格式,宽度和高度。
使用这些位图更新图像控件以向用户呈现视频,但我不确定最有效的方法是什么,大部分时间CPU使用率超过75%,内存从40MB变为500MB,然后GC开始工作,再次降至40MB。应用程序开始变得无响应。
我该怎么办?
谢谢!
你很可能正在分配新的Bitmap,这些Bitmap不可回收。你应该只分配一个WriteableBitmap
并更新它。链接的文档描述了锁定、更新和解锁WriteableBitmap
的过程。
在我工作的软件中使用WPF实时超声图像,我接收到一个Windows Forms Bitmap,然后使用本地CopyMemory方法直接将其复制到WriteableBitmap中。即使进行这种更复杂的操作,CPU也不会太紧张,只要我适当地处理可以回收的内容,内存使用量就不会变化。希望这个例子能帮到你:
// DLL returns images as a WinForms Bitmap
Bitmap bmp = myClass.getWinFormsBitmap();
// In my situation, the images are always 640 x 480.
BitmapData data = bmp.LockBits(new Rectangle(0, 0, 640, 480), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
this.writeableBitmap.Lock();
// Copy the bitmap's data directly to the on-screen buffers
NativeMethods.CopyMemory(this.writeableBitmap.BackBuffer, data.Scan0, ImageBufferSize);
// Moves the back buffer to the front.
this.writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, 640, 480));
this.writeableBitmap.Unlock();
bmp.UnlockBits(data);
// Free up the memory of the WinForms bitmap
bmp.Dispose();
CopyMemory的定义如下:
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);
// DLL returns images as a WinForms Bitmap
// It's disposed even if an exception is thrown
using (Bitmap bmp = myClass.getWinFormsBitmap())
{
// In my situation, the images are always 640 x 480.
BitmapData data = bmp.LockBits(new Rectangle(0, 0, 640, 480), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
writeableBitmap.WritePixels(new Int32Rect(0, 0, 640, 480), data.Scan0, ImageBufferSize, data.Stride);
bmp.UnlockBits(data);
}
WritableBitmap
。因为如果您不这样做,在 UI 线程中执行bitmap.Lock()
,例如在CompositionTarget.Rendering
或任何其他事件处理程序中,将会导致 InvalidOperation。 - Unicorn