I'm trying to make a screenshot button that would capture everything inside of the forms.
Initially, I used this code:
Bitmap bmp = new Bitmap(Width, Height);
DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
SaveFileDialog saveDialog = new SaveFileDialog();
if (saveDialog.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate))
{
bmp.Save(fs, ImageFormat.Png);
}
}
And it does the job of capturing form strictly along the borders of the form but fails to capture all labels and buttons.
I found another snippet on SO, that is able to capture all content of the form, but it also captures the area around the form, which is undesired:
Rectangle bounds = Bounds;
Bitmap bmp = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
SaveFileDialog saveDialog = new SaveFileDialog
{
DefaultExt = "png",
FileName = "name")
};
if (saveDialog.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate))
{
bmp.Save(fs,ImageFormat.Png);
}
}
I tried hard, but I can't manage to combine them, to make it capture the form strictly along the boards (as the first snippet does), meanwhile, capturing all content of the form (as the second one)
What I have tried:
Described above.
Tried to use the ClientSize, etc, but I'm not sure I did it correctly, since this is the first time I use it.