我正在使用WPF在剪贴板中显示图像。这种方法在进行Paint和复制时很方便,但是当我点击“Print screen”进行截图时,输出只有一个黑色图像,与屏幕尺寸相同。
怎么回事?
从剪贴板获取数据的代码如下(是的,我确实想使用原始API调用)。
...
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(finalBitmap);
if (LastData == null || source != LastData)
{
tile = new ImageTile();
(tile as ImageTile).Image.Source = source;
tile.RawData = finalBitmap;
}
}
return tile;
...
答案 0 :(得分:0)
我最终使用了内置框架方法,而不是Clipboard.GetImage()。它的WPF版本似乎比Windows Forms版本更好。