我目前正試圖使用writeablebitmap來掃描圖像的IntPtr
,並將每個圖像變成Bitmap
。林想使用WriteableBitmap的,因爲有標準的GDI GDI+ System.Drawing.Bitmap gives error Parameter is not valid intermittently使用來自intptr的可寫位圖創建位圖時使用WritePixels。
問題IM上有一個WriteableBitmap
的方法調用WritePixels
http://msdn.microsoft.com/en-us/library/aa346817.aspx
林不知道我的緩衝區和步幅每一個例子,我設置發現它顯示的步幅爲0,儘管這會引發錯誤。當我將步幅設置爲5時,圖像顯示爲黑色。我知道這可能不是最有效的代碼,但任何幫助將不勝感激。
//create bitmap header
bmi = new BITMAPINFOHEADER();
//create initial rectangle
Int32Rect rect = new Int32Rect(0, 0, 0, 0);
//create duplicate intptr to use while in global lock
dibhand = dibhandp;
bmpptr = GlobalLock(dibhand);
//get the pixel sizes
pixptr = GetPixelInfo(bmpptr);
//create writeable bitmap
var wbitm = new WriteableBitmap(bmprect.Width, bmprect.Height, 96.0, 96.0, System.Windows.Media.PixelFormats.Bgr32, null);
//draw the image
wbitm.WritePixels(rect, dibhandp, 10, 0);
//convert the writeable bitmap to bitmap
var stream = new MemoryStream();
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wbitm));
encoder.Save(stream);
byte[] buffer = stream.GetBuffer();
var bitmap = new System.Drawing.Bitmap(new MemoryStream(buffer));
GlobalUnlock(dibhand);
GlobalFree(dibhand);
GlobalFree(dibhandp);
GlobalFree(bmpptr);
dibhand = IntPtr.Zero;
return bitmap;