开发者

Getting image from clipboard - black when screenshots appear?

I am using WPF to display the image in the clipboard. This works fine when going to Paint and copying something there, but when I hit "Print screen" to take a screenshot, the output there is just a black image, that has the same dimensions as the screen.

How come?

The code for taking data from the clipboard is as follows (yes, I do in fact want to use raw API calls).

...
case CF_BITMAP:
    BitmapSource source = null;
    System.Drawing.Bitmap finalBitmap = null;
    IntPtr destinationHdc = CreateCompatibleDC(IntPtr.Zero);
    if (destinationHdc != null)
    {
        IntPtr sourceHdc = CreateCompatibleDC(IntPtr.Zero);
        if (sourceHdc != null)
        {
            if (OpenClipboard(MainWindow.Handle))
            {
                IntPtr sourceBitmap = GetClipboardData((uint)clipboardFormat);
                SelectObject(sourceHdc, sourceBitmap);

                BITMAP bmp;
                GetObject(sourceBitmap, Marshal.SizeOf(typeof(BITMAP)), out bmp);

                IntPtr destinationBitmap = CreateCompatibleBitmap(destinationHdc, bmp.bmWidth, bmp.bmHeight);
                SelectObject(destinationHdc, destinationBitmap);

                BitBlt(destinationHdc, 0, 0, bmp.bmWidth, bmp.bmHeight, sourceHdc, 0, 0, 0x00CC0020);
                CloseClipboard();

                finalBitmap = System.Drawing.Bitmap.FromHbitmap(destinationBitmap);
            }
            DeleteDC(sourceHdc);
        }
        DeleteDC(destinationHdc);
    }
    if (finalBitmap != null && ((LastData == null || !(LastData is System.Drawing.Bitmap)) || !finalBitmap.EqualsTo((System.Drawing.Bitmap)LastData)))
    {
        source = BitmapToSource(fina开发者_开发知识库lBitmap);

        if (LastData == null || source != LastData)
        {
            tile = new ImageTile();
            (tile as ImageTile).Image.Source = source;
            tile.RawData = finalBitmap;
        }
    }
    return tile;
...


I ended up using the inbuilt framework method instead, Clipboard.GetImage(). The WPF version of it seems to work better than the Windows Forms one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜