WPF 2D 高性能图形

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

如果你正在进行逐像素的操作,你真的需要WPF吗? - MusiGenesis
整个应用程序的上下文是WPF。 - Klay
2个回答

5

这是我找到的:

我创建了一个继承自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有一个名为WritePixels的方法,它将无符号整数数组作为像素数据。我将图像的源设置为WriteableBitmap。现在,当我更新像素数据并调用WritePixels时,它会更新图像。
我将业务点数据存储在单独的对象中,作为点列表。我对列表执行变换,并使用变换后的点更新像素数据。这样可以避免几何对象的开销。
只是提醒一下,我使用Bresenham算法绘制线条连接我的点。
这种方法非常快速。我正在响应鼠标移动更新大约50,000个点(和连接的线),没有明显的延迟。

1

喜欢博客上的这句话:“……WPF在图像方面的性能很差”。 - MusiGenesis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接