2009-09-28 88 views
7

基本上,我想要WPF中的GDI類型的功能,我可以將像素寫入位圖並通過WPF更新並顯示該位圖。請注意,我需要通過更新像素以響應鼠標移動來動態激活位圖。我讀過InteropBitmap對此非常完美,因爲您可以寫入內存中的像素並將內存位置複製到位圖中 - 但我沒有任何好的示例。wpf 2d高性能圖形

有誰知道任何好的資源,教程或博客使用InteropBitmap或其他類在WPF中執行高性能2D圖形嗎?

+0

如果你正在做逐像素的東西,你真的需要WPF嗎? – MusiGenesis 2009-09-28 17:26:21

+0

應用程序其餘部分的上下文是WPF。 – Klay 2009-10-01 16:01:34

回答

4

這裏是我發現的:

我創建了一個類,它的子類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時,它會更新圖像。

我將業務點數據作爲點列表存儲在單獨的對象中。我在列表上執行變換,並用變換的點更新像素數據。這樣幾何對象就沒有開銷。

只是供參考,我用線條連接我的點使用所謂的Bresenham算法。

這種方法是極其快。爲了響應鼠標移動,我正在更新大約50,000點(和連接線),沒有明顯的滯後。

1

這是一篇關於使用Web cameras with InteropBitmap的博客文章。它包含一個完整的源代碼項目,演示InteropBitmap的用法。

+0

喜歡這個博客的引用:「...... WPF在成像方面的表現很糟糕」。 – MusiGenesis 2009-09-28 17:25:12