0

I have been trying to take a of a section of my window and save it as a png. So far I have managed to screenshot the section but the image it creates is the size of my monitors and all transparent apart from the section I have been trying to screenshot.[enter image description here][1]

The image below I opened the screenshot in paint so you can see what I mean:

https://i.stack.imgur.com/gtyee.png

Is there anyway I can just cut out the section I need? Or am I doing something wrong?

This is what I have atm:

private void CaptureMyScreen()
{
    double screenLeft = SystemParameters.PrimaryScreenWidth;
    double screenTop = SystemParameters.PrimaryScreenHeight;
    double screenWidth = SystemParameters.VirtualScreenWidth;
    double screenHeight = SystemParameters.VirtualScreenHeight;

    using (Bitmap bmp = new Bitmap((int)screenWidth, (int)screenHeight))
    {
        using (Graphics g = Graphics.FromImage(bmp))
        {
            Opacity = .0;

            int screen1width = (int)SystemParameters.PrimaryScreenWidth / 1000;
            int screen1height = (int)SystemParameters.PrimaryScreenHeight / 1000;

            int primaryScreenWidth = (int)SystemParameters.PrimaryScreenWidth;
            int virtualScreenWidth = (int)SystemParameters.VirtualScreenWidth;

            int primaryScreenHeight = (int)SystemParameters.PrimaryScreenHeight;
            int virtualScreenHeight = (int)SystemParameters.VirtualScreenHeight;

            int c1 = primaryScreenWidth / 2 + (screen1width * 253);
            int c2 = primaryScreenHeight - (screen1height * 1400);

            int c3 = c1 + virtualScreenWidth - primaryScreenWidth;
            int c4 = c2 + (virtualScreenHeight - primaryScreenHeight) 
                - (screen1height * 245);

            //280
            //646
            double sizeWidth = primaryScreenWidth * 0.24;
            double sizeHeight = primaryScreenHeight * 0.88; 

            // sizeWidth, sizeHeight
            g.CopyFromScreen(c1, c2, c3, c4, 
                new System.Drawing.Size(Convert.ToInt32(sizeWidth), 
                Convert.ToInt32(sizeHeight)));

            OutputOCR.Text = ((int)SystemParameters.PrimaryScreenWidth).ToString();
            bmp.Save(@"C:\capture.png");
            Opacity = 1;
        }
    }
}

I'm relatively sure my code is inefficient. I've also been trying to do everything in a total % of the screen size so it will work with different resolutions, and with/without multiple monitors. If there is a way to just screenshot a window by name it would remove the multiple monitors issue but I couldn't find a possible way to do that.

jamesnet214
  • 1,044
  • 13
  • 21
Ted Doi
  • 29
  • 5
  • There are a number of different possibilities. See duplicate. For WPF, possibly the best approach if what you want is to get the image of a `Window` object in your own process is to just use `RenderTargetBitmap` and render the `Window` object to the bitmap (which you can do because `Window` is itself a `Visual` instance). – Peter Duniho May 18 '21 at 22:14
  • The window I'm screenshotting is a separate process, a game. So I don't believe this would work. – Ted Doi May 18 '21 at 22:19
  • You're right, `RenderTargetBitmap` doesn't work in that case. However, the duplicate has _lots_ of other options, most of which _do_ work when dealing with external processes. – Peter Duniho May 18 '21 at 22:19
  • I have been looking through threads similar to this one all day, tried many different methods people posted, each one there were multiple issues that I couldn't fix, such has .screen or .bounds not meaning anything to the program. – Ted Doi May 18 '21 at 22:28

0 Answers0