簡體   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