WPF: How to efficiently update an Image 30 times per second WPF: How to efficiently update an Image 30 times per second wpf wpf

WPF: How to efficiently update an Image 30 times per second


You're most likely allocating new Bitmaps, which aren't disposable. You should allocate a single WriteableBitmap and update that instead. The linked documentation describes the process behind locking, updating, and unlocking a WriteableBitmap

On software I work on using live ultrasound images in WPF, I am receiving a Windows Forms Bitmap, which I copy into the WriteableBitmap directly using the native CopyMemory method. Even with this more complicated work, the CPU isn't strained too hard, and the memory usage never moves as long as I properly dispose what I can. Hopefully this example can help you:

// DLL returns images as a WinForms BitmapBitmap 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 buffersNativeMethods.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 bitmapbmp.Dispose();

Where CopyMemory is defined as:

[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);


Using the convenience method called WritePixels on WriteableBitmap we can write it a bit shorter like so:

// DLL returns images as a WinForms Bitmap// It's disposed even if an exception is thrownusing (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);}