问题描述
基本上,我想在WPF,在那里我可以写像素的位图和更新,并显示位图通过WPF GDI类型的功能。请注意,我需要能够通过响应鼠标移动更新像素的动画在运行中的位图。我读过InteropBitmap非常适合这一点,你可以写像素内存和存储位置复制到该位图 - 但我没有任何很好的例子去了
Basically, I want GDI-type functionality in WPF, where I can write pixels to a bitmap and update and display that bitmap through WPF. Note, I need to be able to animate the bitmap on the fly by updating pixels in response to mouse movements. I've read that InteropBitmap is perfect for this, as you can write to pixels in memory and copy the memory location to the bitmap--but I don't have any good examples to go by.
有谁知道有什么好的资源,教程或博客的使用InteropBitmap或其他类做高性能2D图形在WPF?
Does anyone know of any good resources, tutorials, or blogs for using the InteropBitmap or some other classes for doing high-performance 2D graphics in WPF?
推荐答案
下面是我发现:
我创建了一个子类图像的类。
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的有一个称为WritePixels方法,它以无符号整数作为像素数据的数组。我设置图像的来源是WriteableBitmap的。现在,当我更新的像素数据,并调用WritePixels,它更新的形象。
WriteableBitmap has a method called WritePixels, which takes an array of unsigned ints as pixel data. I set the source of the image to be the WriteableBitmap. Now, when I update the pixel data and call WritePixels, it updates the image.
我存储在一个单独的对象为点列表的营业点的数据。我的列表上执行转换,并更新与该转换后的点的像素数据。这种方式有没有从几何对象的开销。
I store the business point data in a separate object as a List of Points. I perform transformations on the list, and update the pixel data with the transformed points. This way there's no overhead from Geometry objects.
仅供参考,我连我的观点,使用一种叫布氏算法绘制的线条。
Just FYI, I connect my points with lines drawn using something called Bresenham's algorithm.
此方法的非常的快。我更新约50,000点(和连接线),以响应鼠标动作,没有明显的滞后性。
This method is extremely fast. I'm updating about 50,000 points (and connecting lines) in response to mouse movements, with no noticeable lag.
这篇关于WPF 2D高性能图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!