如何抓取图像并将其保存在文件夹中

我正在创build一个Windows应用程序使用c#。我有一个button,应该捕获图像(整个桌面屏幕),并将其保存在一个文件夹中。我还需要显示图像的预览。

Graphics.CopyFromScreen方法

示例代码:

Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size); g.Save(); bmp.Save("D:\\file.jpg", ImageFormat.Bmp); 

至于展示预览。 国际海事组织不是很难写你自己的。

有不同的方式来执行你在这里带来的。 使用Screen类,我在互联网上找到了一些简单的例子。 其他人正在使用Direct3D。

  1. TeboScreen:基本的C#屏幕捕捉应用程序 ;
  2. 捕捉屏幕截图 ;
  3. C# – Direct3D的屏幕截图 ;
  4. 捕获桌面屏幕 ;
  5. 使用C#和Windows窗体在.NET中增强桌面记录器 ; (也许不适合你的问题,但如果你计划进一步的功能可能会变得有趣。)
  6. 使用C#捕获屏幕图像 。

总之,这个想法包括使用Screen类或者您喜欢的方式获取桌面的图像,将其存储到Bitmap对象中并将该位图保存到文件中。

至于显示预览,一旦你的Bitmap实例被创建,你只需要一个PictureBox并设置它的Image属性,并向用户显示你的表单,以便他可以看到图像。

希望这可以帮助! =)

你将需要做一些Interop dll的导入。

看看下面的例子,非常好地显示如何捕获屏幕截图并保存到磁盘。

 public void CaptureScreen(string fileName,ImageFormat imageFormat) { int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()), hdcDest = GDI32.CreateCompatibleDC(hdcSrc), hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8), GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020); SaveImageAs(hBitmap,fileName,imageFormat); Cleanup(hBitmap,hdcSrc,hdcDest); } 

上面的例子取自网站。 Perry Lee的所有代码