简体   繁体   English

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

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

I am Creating an windows application using c#.I have a button which should capture the image(the entire Desktop screen) and Save it in a folder.Also i need to show the preview of the image.我正在使用 c# 创建一个 windows 应用程序。我有一个按钮应该捕获图像(整个桌面屏幕)并将其保存在文件夹中。我还需要显示图像的预览。

Graphics.CopyFromScreen Method Graphics.CopyFromScreen 方法

sample code:示例代码:

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

as for show the preview.至于显示预览。 IMO not that hard to write it on ur own. IMO 自己编写它并不难。

There are different ways to perform what you bring here.有不同的方式来执行你带来的东西。 Using the Screen class, there are a few simple samples I found on the Internet.使用Screen class,我在网上找到了几个简单的示例。 Others are using Direct3D.其他人正在使用 Direct3D。

  1. TeboScreen: Basic C# Screen Capture Application ; TeboScreen:基本 C# 屏幕捕获应用程序
  2. Capture a Screen Shot ;捕获屏幕截图
  3. C# – Screen capture with Direct3D ;C# – 使用 Direct3D 进行屏幕截图
  4. Capture DeskTop Screen ;捕获桌面屏幕
  5. Enhanced Desktop Recorder in .NET using C# and Windows Forms ; .NET 中使用 C# 和 Windows Z6450242531912981C3683CAE88A32A66 的增强型桌面记录器(perhaps not suited for your question, but might get interesting if you plan further features.) (可能不适合您的问题,但如果您计划更多功能,可能会变得有趣。)
  6. Capturing the Screen Image Using C# . 使用 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. 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.

As for displaying a preview, once your Bitmap instance is created, you simply need a PictureBox and set its Image property and show your form to the user so he may see the image.至于显示预览,一旦创建了Bitmap实例,您只需要一个PictureBox并设置其Image属性并向用户显示您的表单,以便他可以看到图像。

Hope this helps!希望这可以帮助! =) =)

You will need to do some importing of Interop dlls.您将需要导入一些 Interop dll。

Take a look at the following example shows very well how to capture the screen shot and save to disk.看看下面的例子很好地展示了如何捕获屏幕截图并保存到磁盘。

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

The above example taken from the website.以上示例取自网站。 All code by Perry Lee Perry Lee 的所有代码

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

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