2012-05-08 57 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