2010-07-22 43 views
15

的部分截圖像C#:如何採取屏幕

TakeScreenshot(new Rectangle(0,0,100,100), "output.jpg"); 
+2

你需要指定是的WinForms,WPF或Silverlight。 – alxx 2010-07-22 07:22:26

+0

我試圖在類庫中創建此方法 – 2010-07-22 07:28:02

回答

24

使用以下命令:

 Rectangle rect = new Rectangle(0, 0, 100, 100); 
     Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 
     Graphics g = Graphics.FromImage(bmp); 
     g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 
     bmp.Save(fileName, ImageFormat.Jpeg); 
+0

什麼是「PixelFormat.Format32bppArgb」? – 2010-07-22 07:35:12

+1

PixelFormat.Format32bppArgb指定格式爲每像素32位;每個8位用於alpha,紅色,綠色和藍色分量。 – 2010-07-22 07:53:50

+0

謝謝!這是關閉我需要什麼,所以我接受這個答案:) – 2010-07-22 08:06:39

12

這裏是捕獲屏幕的代碼。將值更改爲您需要的大小。

Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

Graphics graphics = Graphics.FromImage(printscreen as Image); 

graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

printscreen.Save(@"C:\printscreen.jpg", ImageFormat.Jpeg); 

或做方法,該方法將返回捕捉這樣的形象:

Image CaptureScreen(int sourceX, int sourceY, int destX, int destY, 
      Size regionSize) 
{ 
    Bitmap bmp = new Bitmap(regionSize.Width, regionSize.Height); 
    Graphics g = Graphics.FromImage(bmp); 
    g.CopyFromScreen(sourceX, sourceY, destX, destY, regionSize); 
    return bmp; 
} 
...... 
// call 
Image image = CaptureScreen(sourceX, sourceY, destX, destY, regionSize); 
image.Save(@"C:\Somewhere\screen.jpg);