如何从剪贴板获取截图?
问题描述:
我尝试从剪贴板中获取屏幕截图。我写了以下PRINT-Key触发的代码:如何从剪贴板获取截图?
IDataObject obj = Clipboard.GetDataObject();
var formats = obj.GetFormats();
但“格式”为空。 www中的所有教程都告诉我这样做,但我不能解释上述行为。
(我使用C#.NET藤条4.0在Windows 7 64位系统)
答
发现我的keylistener阻止了windows-keylistener。这也是为什么窗口haven't在剪贴板中保存的截图:-(
解决方法:
// get the bounding area of the screen containing (0,0)
// remember in a multidisplay environment you don't know which display holds this point
Drawing.Rectangle bounds = Forms.Screen.GetBounds(System.Drawing.Point.Empty);
// create the bitmap to copy the screen shot to
Drawing.Bitmap bitmap = new Drawing.Bitmap(bounds.Width, bounds.Height);
// now copy the screen image to the graphics device from the bitmap
using (Drawing.Graphics gr = Drawing.Graphics.FromImage(bitmap))
{
gr.CopyFromScreen(Drawing.Point.Empty, Drawing.Point.Empty, bounds.Size);
}
答
您是否尝试过的方法Clipboard.GetImage();
?
Image GetClipboardImage()
{
if (Clipboard.ContainsImage())
{
return Clipboard.GetImage();
}
// add error handling
}
听起来像你对我需要另一个PRINT键,你从来没有说过“但其他程序可以看到它。” –
以及我想在我的帖子中解释,截图是因为我可以通过它来绘制,但我希望它很明显;-) – jwillmer