2011-08-26 133 views
0

我尝试从剪贴板中获取屏幕截图。我写了以下PRINT-Key触发的代码:如何从剪贴板获取截图?

IDataObject obj = Clipboard.GetDataObject(); 
var formats = obj.GetFormats(); 

但“格式”为空。 www中的所有教程都告诉我这样做,但我不能解释上述行为。

(我使用C#.NET藤条4.0在Windows 7 64位系统)

+0

听起来像你对我需要另一个PRINT键,你从来没有说过“但其他程序可以看到它。” –

+0

以及我想在我的帖子中解释,截图是因为我可以通过它来绘制,但我希望它很明显;-) – jwillmer

回答

0

发现我的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); 
} 
2

您是否尝试过的方法Clipboard.GetImage();

Here's the MSDN sample

Image GetClipboardImage() 
{ 
    if (Clipboard.ContainsImage()) 
    { 
     return Clipboard.GetImage(); 
    } 
    // add error handling 
} 
+0

我试过了,没有图像.. – jwillmer

+0

你使用WinForms?我尝试了一个项目中的代码,它工作正常。 – Lander

+0

使用wpf,也许多数民众赞成但它仍然是为什么? – jwillmer

1

你有没有打过电话的GetImage方法?

image = Clipboard.GetImage(); 

有在链路提供了更多的信息,这也说明了如何检查剪贴板上(ContainsImage)存在的图像 - 但对象时,你可能需要采取一些其他步骤,取决于被写入剪贴板。

我甚至没有意识到GetFormats方法,并找不到由Windows窗体或WPF剪贴板类公开的方法。

+0

格式显示剪贴板中的哪些格式,例如:PlainText,RTF,TIFF,PNG,.. – jwillmer