简体   繁体   English

wpf 2d高性能显卡

[英]wpf 2d high performance graphics

Basically, I want GDI-type functionality in WPF, where I can write pixels to a bitmap and update and display that bitmap through WPF. 基本上,我想在WPF中使用GDI类型的功能,在那里我可以将像素写入位图并通过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非常适合这种情况,因为你可以写入内存中的像素并将内存位置复制到位图 - 但我没有任何好的例子。

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? 有没有人知道使用InteropBitmap或其他类在WPF中执行高性能2D图形的任何好的资源,教程或博客?

Here's what I found: 这是我发现的:

I created a class that subclasses Image. 我创建了一个子类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 has a method called WritePixels, which takes an array of unsigned ints as pixel data. WriteableBitmap有一个名为WritePixels的方法,它将无符号整数数组作为像素数据。 I set the source of the image to be the WriteableBitmap. 我将图像的源设置为WriteableBitmap。 Now, when I update the pixel data and call WritePixels, it updates the image. 现在,当我更新像素数据并调用WritePixels时,它会更新图像。

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. 这样,Geometry对象就没有开销。

Just FYI, I connect my points with lines drawn using something called Bresenham's algorithm. 仅供参考,我将我的点与使用Bresenham算法绘制的线条连接起来。

This method is extremely fast. 这种方法非常快。 I'm updating about 50,000 points (and connecting lines) in response to mouse movements, with no noticeable lag. 我正在更新大约50,000点(和连接线)以响应鼠标移动,没有明显的滞后。

Here's a blog post on using Web cameras with InteropBitmap . 这是一篇关于使用InteropBitmap的网络摄像头的博客文章。 It includes a full source code project demonstrating the InteropBitmap's usage. 它包含一个完整的源代码项目,演示了InteropBitmap的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM