基本上,我希望在WPF中获得类似GDI的功能,可以将像素写入位图并通过WPF更新和显示该位图。请注意,我需要能够通过响应鼠标移动更新像素以实时动画位图。据我所知,InteropBitmap非常适合此操作,因为您可以写入内存中的像素并将内存位置复制到位图,但我没有任何好的示例可供参考。
有人知道使用InteropBitmap或其他类进行WPF高性能2D图形的好资源、教程或博客吗?
有人知道使用InteropBitmap或其他类进行WPF高性能2D图形的好资源、教程或博客吗?
这是我找到的:
我创建了一个继承自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;
}