基本上,我想在WPF中使用GDI类型的功能,在那里我可以将像素写入位图,并通过WPF更新和显示该位图。注意,我需要能够在飞行中通过更新像素来响应鼠标移动的位图动画。我读到过InteropBitmap非常适合这样做,因为您可以写入内存中的像素,并将内存位置复制到位图中--但我没有任何好的示例可供参考。
有没有人知道有什么好的资源、教程或博客,可以使用InteropBitmap或其他一些类在WPF中制作高性能的2D图形?
发布于 2009-10-01 16:20:46
这是我发现的:
我创建了一个子类化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时,它会更新图像。
我将业务点数据作为点列表存储在一个单独的对象中。我对列表执行转换,并用转换后的点更新像素数据。这样就不会产生来自Geometry对象的开销。
仅供参考,我将点与使用Bresenham算法绘制的线连接起来。
这种方法非常快。我更新了大约50,000个点(和连接线)来响应鼠标的移动,没有明显的延迟。
发布于 2009-09-28 16:01:53
这里有一篇关于使用Web cameras with InteropBitmap的博客文章。它包括一个完整的源代码项目,演示InteropBitmap的用法。
https://stackoverflow.com/questions/1487831
复制相似问题