wpf 2d 高性能图形 [英] wpf 2d high performance graphics

查看:34
本文介绍了wpf 2d 高性能图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想要 WPF 中的 GDI 类型功能,在那里我可以将像素写入位图,并通过 WPF 更新和显示该位图.请注意,我需要能够通过响应鼠标移动更新像素来动态地为位图设置动画.我读过 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 或其他一些类在 WPF 中制作高性能 2D 图形的任何好的资源、教程或博客?

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?

推荐答案

这是我的发现:

我创建了一个继承 Image 的类.

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.

我将业务点数据作为点列表存储在一个单独的对象中.我对列表执行转换,并用转换后的点更新像素数据.这样就没有 Geometry 对象的开销.

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.

仅供参考,我将我的点与使用 Bresenham 算法绘制的线连接起来.

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 高性能图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆