Screenshot active area of my form without taskbar and edges outsides of my form
Asked Answered
L

1

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

Lilias answered 31/12, 2017 at 10:19 Comment(4)
var bounds = this.RectangleToScreen(new Rectangle(Point.Empty, this.ClientSize)); You have to declare your app to be dpiAware, the reason your original attempt was off as well. Avoid JPEG for bitmaps containing text, PNG is best.Cassatt
You can also use the Control.DrawToBitmap() method, this doesnt require the form to be in focus for example. Have a look at csharphelper.com/blog/2014/09/…. They have exactly the solution you need. Getting a forms image without border.Gushy
Won't work as intended, he's using the TransparencyKey property.Cassatt
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.Lilias
R
0

I'm trying the same try this code but my problem is

1- getting the actual screen scale and layout to get the actual form size and location for example my screen size 1920 * 1080 with scale 125% when I try to get var screenWidth = SystemParameters.PrimaryScreenWidth; var screenHeight = SystemParameters.PrimaryScreenHeight; I got the result screenWidth = 1920/54 = 1536 , screenHeight = 1080/54 = 864 so till now you will need to adjusted manually like I did

int captureX = pictureBox2.Location.X / 4 * 5 + this.Location.X / 4 * 5; int captureY = pictureBox2.Location.Y / 4 * 5 + this.Location.Y / 4 * 5;

2- CopyFromScreen copy the entire screen including the form so in order to capture the area behind the form you must add opacity = 0 ; then opacity =1 ;
or using this.Visible = false; and this.Visible = true; which causes flickering when moving the form

 private void CaptureResizeAndSetBackgroundpictureBox()
    {
        //// Define the capture area (e.g., the entire screen or a specific region)
        //int captureX = pictureBox2.Location.X + pictureBox2.Location.X / 4;
        //int captureY = pictureBox2.Location.Y + pictureBox2.Location.Y / 4;
        //int captureWidth = pictureBox2.Width + pictureBox2.Width / 4;  // Width of the form
        //int captureHeight = pictureBox2.Height + pictureBox2.Height / 4; // Height of the form
        this.Visible = false;

        int captureX = pictureBox2.Location.X / 4 * 5 + this.Location.X / 4 * 5;
        int captureY = pictureBox2.Location.Y / 4 * 5 + this.Location.Y / 4 * 5;
        int captureWidth = pictureBox2.Width + pictureBox2.Width / 4;  // Width of the form
        int captureHeight = pictureBox2.Height + pictureBox2.Height / 4; // Height of the form

        // Capture the screen within the defined area
        // Bitmap capturedImage = new Bitmap(captureWidth, captureHeight);
        Bitmap capturedImage = new Bitmap(captureWidth, captureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics captureGraphics = Graphics.FromImage(capturedImage))
        {
            captureGraphics.CopyFromScreen(captureX, captureY, 0, 0, new Size(captureWidth, captureHeight));
        }

        // Resize the captured image if needed (optional)
        int newWidth = pictureBox2.Width; // Set your desired new width
        int newHeight = pictureBox2.Height; // Set your desired new height
        if (capturedImage.Width != newWidth || capturedImage.Height != newHeight)
        {
            capturedImage = new Bitmap(capturedImage, newWidth, newHeight);
        }
        // capturedImage.MakeTransparent(capturedImage.GetPixel(this.Location.X, this.Location.Y));
        // Set the resized image as the background of the form
        pictureBox2.Image = capturedImage;
        this.Visible = true;
    }
Rutilant answered 15/10, 2023 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.