Need to edge out the competition for your dream job? Train for certifications today.
/// Returns an image of the rendered item as a Bitmap object.
/// It is up to the caller of this method to dispose of the Bitmap
/// object returned when the caller is finished with it.
/// A Bitmap object containing the rendering of the current item.
public Bitmap GetItemAsBitmap()
// NOTE: You cannot wrap the Bitmap declaration in a using
// statement as that causes the Bitmap object to be
// disposed before the caller receives it. It is up to
// the caller to dispose of the Bitmap.
// Get a reference to the drawing surface of the control
using( Graphics source = this.CreateGraphics() )
// Create a bitmap that is the same width, height, and
// resolution as the control
Bitmap bitmap = new Bitmap( this.Width, this.Height, source );
// Create a graphics object on the blank bitmap so we can
// draw into it
using( Graphics target = Graphics.FromImage( bitmap ) )
// Get the screen coordinates of this control; we are looking
// for a point relative to the top left of the control (this)
// offset by x = 0 and y = 0 (Point.Empty)
Point screen = this.PointToScreen( Point.Empty );
// Copy from the screen into the bitmap (via the Graphics
target.CopyFromScreen( screen.X, screen.Y, 0, 0, this.Size );
Open in new window
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.