2

in Winform application, i have a user control; on which shapes like rectangle,circle etc. are drawn. i am trying to take snapshot of the same by using DrawToBitmap() method. i have a Bitmap of fixed size(300 x 300) and user control is of size (600 x 800) so the taken snapshot contains only part of the user control.

how to get the snapshot of entire user control in the bitmap ? thanks in advance.

Iorn Man
  • 267
  • 8
  • 21

1 Answers1

4

You can use the following approach:

static void DrawControlToImage(Control ctrl, Image img) {
    Rectangle sourceRect = ctrl.ClientRectangle;
    Size targetSize = new Size(img.Width, img.Height);
    using(Bitmap tmp = new Bitmap(sourceRect.Width, sourceRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
        ctrl.DrawToBitmap(tmp, sourceRect);
        using(Graphics g = Graphics.FromImage(img)) {
            g.DrawImage(tmp, new Rectangle(Point.Empty, targetSize));
        }
    }
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53