【发布时间】:2011-04-20 13:24:06
【问题描述】:
我目前正在尝试使用 writeablebitmap 对图像扫描进行IntPtr 并将每个图像转换为Bitmap。我想使用 writeablebitmap,因为我遇到了标准 gdi 的问题
GDI+ System.Drawing.Bitmap gives error Parameter is not valid intermittently
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;
【问题讨论】:
标签: c# image-processing bitmap scanning