Wpf 2d high-performance graphics

Basically, I want GDI type functionality in WPF, where I can write pixels to a bitmap and update and display that bitmap through WPF. Please note: I need to be able to animate a bitmap on the fly, updating the pixels in response to mouse movements. I read that InteropBitmap is perfect for this, since you can write in pixels in memory and copy the memory location to a bitmap - but I have no good examples.

Does anyone know of any good resources, tutorials or blogs for using InteropBitmap or some other classes to perform high-performance 2D graphics in WPF?

+7
performance wpf graphics 2d
source share
2 answers

Here is what I found:

I created a class that subclasses Image.

public class MyImage : Image { // the pixel format for the image. This one is blue-green-red-alpha 32bit format private static PixelFormat PIXEL_FORMAT = PixelFormats.Bgra32; // the bitmap used as a pixel source for the image WriteableBitmap bitmap; // the clipping bounds of the bitmap Int32Rect bitmapRect; // the pixel array. unsigned ints are 32 bits uint[] pixels; // the width of the bitmap. sort of. int stride; public MyImage(int width, int height) { // set the image width this.Width = width; // set the image height this.Height = height; // define the clipping bounds bitmapRect = new Int32Rect(0, 0, width, height); // define the WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PIXEL_FORMAT, null); // define the stride stride = (width * PIXEL_FORMAT.BitsPerPixel + 7) / 8; // allocate our pixel array pixels = new uint[width * height]; // set the image source to be the bitmap this.Source = bitmap; } 

WriteableBitmap has a method called WritePixels that takes an array of unsigned ints as pixel data. I set the image source as WriteableBitmap. Now when I update the pixel data and call WritePixels, it updates the image.

I store business point data in a separate object as a list of points. I do the conversions in the list and update the pixel data with the converted points. Thus, there is no overhead from geometry objects.

Just FYI, I connect my points with lines drawn using the Bresenham algorithm.

This method is extremely fast. I update about 50,000 points (and connecting lines) in response to mouse movements without noticeable lag.

+4
source share

Here's a blog post about using webcams with InteropBitmap . It includes a complete source code project demonstrating the use of InteropBitmap.

+1
source share

All Articles