How to get a screenshot using UserControl?

I want to get a screenshot of MainForm of Application. For this, I use the following code.

Bitmap bitmap = new Bitmap(MainForm.Width, MainForm.Height);
DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
pictureBox1.Image = bitmap;

I have some user controls in MainForm. I can only get a screenshot of MainForm using the code above. I can not get a screenshot of User Controls.

Can anyone suggest me how to get a screenshot of a windows form using form user controls?

+4
source share
1 answer

This should do it:

private void CaptureForm()
{
    var bmp = new Bitmap(MainForm.Width, MainForm.Height, PixelFormat.Format32bppArgb);

    var g = Graphics.FromImage(bmp);
    g.CopyFromScreen(MainForm.Location.X, MainForm.Location.Y, 0, 0,
                    MainForm.Size, CopyPixelOperation.SourceCopy);

    //Clipboard.SetImage(bmp);

    pictureBox1.Image = bmp;
}
+1
source

Source: https://habr.com/ru/post/1621947/


All Articles