2008-12-12 72 views
55

我想要捕獲屏幕以獲取圖像 - 就像使用鍵盤上的「打印屏幕」按鈕一樣。將屏幕捕獲到位圖

有沒有人有一個想法如何做到這一點?我沒有起點。

回答

95

如果使用.NET 2.0(或更高版本)的框架,你可以使用CopyFromScreen()方法這裏詳述:

http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

//Create a new bitmap. 
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
           Screen.PrimaryScreen.Bounds.Height, 
           PixelFormat.Format32bppArgb); 

// Create a graphics object from the bitmap. 
var gfxScreenshot = Graphics.FromImage(bmpScreenshot); 

// Take the screenshot from the upper left corner to the right bottom corner. 
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, 
          Screen.PrimaryScreen.Bounds.Y, 
          0, 
          0, 
          Screen.PrimaryScreen.Bounds.Size, 
          CopyPixelOperation.SourceCopy); 

// Save the screenshot to the specified path that the user has chosen. 
bmpScreenshot.Save("Screenshot.png", ImageFormat.Png); 
+0

不錯和簡單...就像一個魅力...感謝! – 2009-10-10 13:04:38

+1

你的回答不正確,請與(g.CopyFromScreen(更新 0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); – 2013-01-27 06:12:41

+1

但是否多顯示器上工作PC – EaterOfCode 2013-07-31 18:31:27

5
// Use this version to capture the full extended desktop (i.e. multiple screens) 

Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, 
           SystemInformation.VirtualScreen.Height, 
           PixelFormat.Format32bppArgb); 
Graphics screenGraph = Graphics.FromImage(screenshot); 
screenGraph.CopyFromScreen(SystemInformation.VirtualScreen.X, 
          SystemInformation.VirtualScreen.Y, 
          0, 
          0, 
          SystemInformation.VirtualScreen.Size, 
          CopyPixelOperation.SourceCopy); 

screenshot.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png); 
0

嘗試this代碼

Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 
Graphics gr = Graphics.FromImage(bmp); 
gr.CopyFromScreen(0, 0, 0, 0, bmp.Size); 
pictureBox1.Image = bmp; 
bmp.Save("img.png",System.Drawing.Imaging.ImageFormat.Png); 
0
Bitmap memoryImage; 
//Set full width, height for image 
memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
         Screen.PrimaryScreen.Bounds.Height, 
         PixelFormat.Format32bppArgb); 
Size s = new Size(memoryImage.Width, memoryImage.Height); 
Graphics memoryGraphics = Graphics.FromImage(memoryImage); 
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s); 
string str = ""; 
try 
{ 
    str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + 
      @"\Screenshot.png");//Set folder to save image 
} 
catch { }; 
memoryImage.save(str);