C#: how to take a screenshot of a portion of screen

后端 未结 4 1001
不思量自难忘°
不思量自难忘° 2020-12-01 09:12

like

TakeScreenshot(new Rectangle(0,0,100,100), \"output.jpg\");
4条回答
  •  醉酒成梦
    2020-12-01 09:56

    Here is the code to capture the screen. Change the values to the size you need.

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

    Or make method which will return you captured image like this :

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

提交回复
热议问题