I want to capture only a predefined region of the application, for example I want to print screen only the group box. I changed this.bounds to groupbox.bounds but it doesn't work. It captures other regions but not the group box. Any ideas? The codes are:
// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "Select output file:";
saveImageDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//saveImageDialog.FileName = printFileName;
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{enter code here
// Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(saveImageDialog.FileName, ImageFormat.Png);
}
Thanks.
The problem is that the coordinates in the Bounds
property are relative to its parent, but CopyFromScreen
needs absolute coordinates.
Try using the PointToScreen method:
Point p = this.PointToScreen(new Point(groupBox.Bounds.X, groupBox.Bounds.Y));
gfxScreenshot.CopyFromScreen(p.X, p.Y, 0, 0,
groupBox.Bounds.Size, CopyPixelOperation.SourceCopy);