2

I am trying to capture a screenshot of a windows form application, but when I capture the screenshot I get the taskbar at the top and edges outsides of my application. I just want to capture a screenshot of the inner part of my application excluding the outer edge outside of my application and the taskbar. Here is the code I am using to create a screenshot of the application.

    private void captureScreenshot()
    {
        Rectangle bounds = this.Bounds;
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            }
            bitmap.Save("c:\\Users\\Brandon\\Downloads\\Test.jpg", ImageFormat.Jpeg);
        }
    }

I'm assuming that the bounds of the application is not exactly what I want to be screenshot to get the effect that I desire as it captures the taskbar and slightly outside the edge of my application.

Here is a picture of the screenshot I am getting:

enter image description here

Here is a picture of the area that I would like to screenshot (outlined in yellow) :

enter image description here

Enryu
  • 1,406
  • 1
  • 14
  • 26
  • 2
    var bounds = this.RectangleToScreen(new Rectangle(Point.Empty, this.ClientSize)); You have to declare your app [to be dpiAware](https://stackoverflow.com/a/13228495/17034), the reason your original attempt was off as well. Avoid JPEG for bitmaps containing text, PNG is best. – Hans Passant Dec 31 '17 at 11:31
  • You can also use the Control.DrawToBitmap() method, this doesnt require the form to be in focus for example. Have a look at http://csharphelper.com/blog/2014/09/get-the-image-of-a-control-or-form-or-a-forms-client-area-in-c/. They have exactly the solution you need. Getting a forms image without border. – Felix Almesberger Dec 31 '17 at 11:38
  • Won't work as intended, he's using the TransparencyKey property. – Hans Passant Dec 31 '17 at 11:54
  • Thanks Hans Passant, your solution worked just fine. I guess my application must have already been dpi aware because it just worked once I added your code. I'm using jpeg because I am sending the images to google to analyze with machine learning and that is the only purpose for the images so they need to be small and don't need to be high quality. If you want, you can post your comment as an answer and I will mark it as correct. – Enryu Jan 01 '18 at 01:50

0 Answers0