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.