我有一個數組,如byte[] pixels
。有沒有辦法在不復制數據的情況下從pixels
創建一個bitmap
對象?我有一個小圖形庫,當我需要在WinForms
窗口上顯示圖像時,我只需將該數據複製到一個bitmap
對象,然後使用draw方法。我可以避免這種複製過程嗎?我記得我在某處看到過它,但也許我的記憶力很差。從數組創建位圖對象
編輯:我試過這個代碼,它的工作原理,但這是安全的嗎?
byte[] pixels = new byte[10 * 10 * 4];
pixels[4] = 255; // set 1 pixel
pixels[5] = 255;
pixels[6] = 255;
pixels[7] = 255;
// do some tricks
GCHandle pinnedArray = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// create a new bitmap.
Bitmap bmp = new Bitmap (10, 10, 4*10, PixelFormat.Format32bppRgb, pointer);
Graphics grp = this.CreateGraphics();
grp.DrawImage (bmp, 0, 0);
pixels[4+12] = 255; // add a pixel
pixels[5+12] = 255;
pixels[6+12] = 255;
pixels[7+12] = 255;
grp.DrawImage (bmp, 0, 40);
這是有點相關我想:http://stackoverflow.com/questions/1580130/high-speed-performance c-sharp-image-filtering-in-c-sharp – Patrick 2012-08-16 14:52:58