wpf 2d高性能图形
基本上,我想要WPF中的GDI类型功能,在WPF中我可以将像素写入位图,并通过WPF更新和显示该位图。注意,我需要能够动态地通过更新像素来响应鼠标移动,从而为位图设置动画。我已经读到InteropBitmap非常适合这样做,因为您可以写入内存中的像素,并将内存位置复制到位图中——但我没有任何好的示例可供参考wpf 2d高性能图形,wpf,performance,graphics,2d,Wpf,Performance,Graphics,2d,基本上,我想要WPF中的GDI类型功能,在WPF中我可以将像素写入位图,并通过WPF更新和显示该位图。注意,我需要能够动态地通过更新像素来响应鼠标移动,从而为位图设置动画。我已经读到InteropBitmap非常适合这样做,因为您可以写入内存中的像素,并将内存位置复制到位图中——但我没有任何好的示例可供参考 有人知道有什么好的资源、教程或博客可以使用InteropBitmap或其他一些类在WPF中制作高性能2D图形吗 这里有一篇关于使用的博客文章。它包括一个完整的源代码项目,演示InteropB
有人知道有什么好的资源、教程或博客可以使用InteropBitmap或其他一些类在WPF中制作高性能2D图形吗 这里有一篇关于使用的博客文章。它包括一个完整的源代码项目,演示InteropBitmap的用法。以下是我的发现: 我创建了一个类,对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算法绘制的线连接起来
这种方法非常快。我正在更新大约50000个点(和连接线),以响应鼠标移动,没有明显的延迟。喜欢博客中的这句话:“……WPF在成像方面的性能太差了”。如果你在做逐像素的工作,你真的需要WPF吗?应用程序其余部分的上下文是WPF。