2012-12-27 67 views
2

在Winform应用程序中,我有一个用户控件;绘制矩形,圆形等形状。 我想通过使用DrawToBitmap()方法拍摄相同的快照。 我有一个固定大小的位图(300 x 300)和用户控件的大小(600 x 800) ,因此拍摄的快照仅包含用户控件的一部分。control.DrawToBitmap()不按预期工作

如何获取位图中整个用户控件的快照? 在此先感谢。

回答

3

您可以用下面的办法:

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)); 
     } 
    } 
}