Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 18-Feb-22 9:20am

I have write the same functionality using below link. Check for
c# - How to get the screenshot of the form - Stack Overflow[^]
 
Share this answer
 
Comments
John Bon Jovy 13-Feb-22 1:57am    
I saw it, but it doesn't capture all controls on the form. Half of the labels are not being captured
M Imran Ansari 13-Feb-22 2:57am    
Just to check the behaviour of code, could you please share the screenshot?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900