我目前正在尝试使用 writeablebitmap 对IntPtr
图像进行扫描并将每个图像转换为Bitmap
. 我想使用 writeablebitmap,因为我对标准 gdi
GDI+ System.Drawing.Bitmap 有问题,会给出错误参数间歇性无效
有一种方法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;