2012-05-08 56 views
0

因此.....标题如下。从Silverlight中的剪贴板获取图像/ PInvoke

虽然我能找到解决方案here

但是解决方法是先将图像写入临时文件并将其读入流中。

所以我一直在试图修改方案,这里是我一直在努力为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来解决这个问题....

任何人都有一个解决方案?

回答

0

此前谷歌,MSDN和实验的时间...

我发现,其实我可以通过使用IntPtr.Zero得到HBITMAP的BITMAPINFO ..

hDC = User32.GetDC(IntPtr.Zero); 
Gdi32.GetDIBits(hDC, hBitmap, 0, 1, null, ref bi, DIB_RGB_COLORS); 

,这将给我一个包含宽度,图像高度的BitmapInfo。

那么现在我可以用宽度和高度caculate字节数组,我需要的尺寸,所以现在我可以使用:

//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,我终于可以将它们映射到我的WriteableBitmap与高度,宽度,字节[] :)

+1

我试图完成相同的目标。你有什么机会可以提供你的解决方案的更多细节? – McAden