Kla*_*lay 7 wpf graphics performance 2d
基本上,我想在WPF中使用GDI类型的功能,在那里我可以将像素写入位图并通过WPF更新并显示该位图.注意,我需要能够通过响应鼠标移动更新像素来动态制作位图动画.我已经读过InteropBitmap非常适合这种情况,因为你可以写入内存中的像素并将内存位置复制到位图 - 但我没有任何好的例子.
有没有人知道使用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;
}
Run Code Online (Sandbox Code Playgroud)
WriteableBitmap有一个名为WritePixels的方法,它将无符号整数数组作为像素数据.我将图像的源设置为WriteableBitmap.现在,当我更新像素数据并调用WritePixels时,它会更新图像.
我将业务点数据存储在单独的对象中作为点列表.我在列表上执行转换,并使用转换后的点更新像素数据.这样,Geometry对象就没有开销.
仅供参考,我将我的点与使用Bresenham算法绘制的线条连接起来.
这种方法非常快.我正在更新大约50,000点(和连接线)以响应鼠标移动,没有明显的滞后.
| 归档时间: |
|
| 查看次数: |
3884 次 |
| 最近记录: |