繁体   English   中英

如何抓取图像并将其保存在文件夹中 [c# windows 应用程序]

[英]How to grab an image and save it in a folder [c# windows application]

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

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);

至于显示预览。 IMO 自己编写它并不难。

有不同的方式来执行你带来的东西。 使用Screen class,我在网上找到了几个简单的示例。 其他人正在使用 Direct3D。

  1. TeboScreen:基本 C# 屏幕捕获应用程序
  2. 捕获屏幕截图
  3. C# – 使用 Direct3D 进行屏幕截图
  4. 捕获桌面屏幕
  5. .NET 中使用 C# 和 Windows Z6450242531912981C3683CAE88A32A66 的增强型桌面记录器 (可能不适合您的问题,但如果您计划更多功能,可能会变得有趣。)
  6. 使用 C# 捕获屏幕图像

In short, the idea consists of getting the image of the desktop using the Screen class or your favorite way, store it into a Bitmap object and save this bitmap into a file.

至于显示预览,一旦创建了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 的所有代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM