所以..。标题是。
尽管我找到了一个解决方案这里。
但解决方案是首先将图像写入临时文件并将其读取到流中。
因此,我一直试图修改解决方案,以下是我在6小时中一直在尝试的案例.:
案例1)
直接从流读取,而使用:
internal static extern int GdipSaveImageToStream(GpImage *image, IStream* stream, GDIPCONST CLSID* clsidEncoder, GDIPCONST EncoderParameters* encoderParams);
它转到:
[DllImport("gdiplus.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]
internal static extern int GdipSaveImageToStream(IntPtr image, IntPtr stream, [In] ref Guid Encoder, IntPtr encoderParams);
我试图将IntPtr.Zero传入流参数,但是IntPtr的输出结果仍然是零.失败。
案例2)
我们试试..。像其中一篇文章所说的那样获取像素数据。
我知道我可以通过调用GetClipboardData获得HBITMAP:
[DllImport("user32.dll")]
static extern IntPtr GetClipboardData(uint uFormat);
因此,让我们尝试使用GetBitMapBits,假设我知道图像的大小:
[DllImport("gdi32.dll")]
static extern int GetBitmapBits(IntPtr hbmp, int cbBuffer, [Out] byte[] lpvBits);
现在至少我能看到数据被复制,但是GetBitMapBits是16位窗口.失败
案例3)
好的,我应该用GetDIBits来获取像素数据.
[DllImport("gdi32.dll")]
public static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint uStartScan,
uint cScanLines, [Out] byte[] lpvBits, ref BitmapInfo lpbmi, uint uUsage);
但现在的问题是..。在那里我可以得到剪贴板的设备上下文.
此外,我如何知道剪贴板图像的宽度和高度……
在WPF/WinForm中,我们可以使用FromHBitmap方法轻松地完成这一任务。除了把文件写进文件并阅读之外,我想不出别的办法了。或者写一个COM来解决这个问题.
有人找到解决办法了吗?
发布于 2012-05-09 18:18:40
经过数小时的google,msdn和实验..。
我发现我实际上可以通过使用BitmapInfo从hBitmap获得IntPtr.Zero。
hDC = User32.GetDC(IntPtr.Zero);
Gdi32.GetDIBits(hDC, hBitmap, 0, 1, null, ref bi, DIB_RGB_COLORS);
这将给我一个BitmapInfo,它包含图像的宽度、高度。
现在,我可以使用宽度和高度来计算我需要的字节数组的大小,所以现在我可以使用:
//Similiarly, hMemDC is also a device context that created using IntPtr.Zero.
Gdi32.GetDIBits(hMemDC, hBitmap, 0, (uint)pixelHeight, binaryData, ref bi, DIB_RGB_COLORS);
有了我的binaryData,我终于可以用高度、宽度、Byte[] :)将它们映射到我的WriteableBitmap上。)
https://stackoverflow.com/questions/10506607
复制相似问题