System.Windows.Forms.Control.DrawToBitmap C# (CSharp) Method

DrawToBitmap() public method

public DrawToBitmap ( Bitmap bitmap, Rectangle targetBounds ) : void
bitmap System.Drawing.Bitmap
targetBounds System.Drawing.Rectangle
return void
		public void DrawToBitmap (Bitmap bitmap, Rectangle targetBounds)
		{
			Graphics g = Graphics.FromImage (bitmap);
			
			// Only draw within the target bouds, and up to the size of the control
			g.IntersectClip (targetBounds);
			g.IntersectClip (Bounds);
			
			// Logic copied from WmPaint
			PaintEventArgs pea = new PaintEventArgs (g, targetBounds);
			
			if (!GetStyle (ControlStyles.Opaque))
				OnPaintBackground (pea);

			OnPaintBackgroundInternal (pea);

			OnPaintInternal (pea);

			//if (!pea.Handled)
				OnPaint (pea);
			
			g.Dispose ();
		}

Usage Example

 /// <summary>
 /// 
 /// </summary>
 /// <param name="panel"></param>
 public static void GenerateImage(Control panel)
 {
     //Save control images for documentation purposes
     var image = new Bitmap(panel.ClientRectangle.Width, panel.ClientRectangle.Height);
     panel.DrawToBitmap(image, panel.ClientRectangle);
     image.Save(panel.Name + ".png");
 }
All Usage Examples Of System.Windows.Forms.Control::DrawToBitmap
Control