Skip to content

Instantly share code, notes, and snippets.

@afrael
Last active December 17, 2015 20:59
Show Gist options
  • Save afrael/5671353 to your computer and use it in GitHub Desktop.
Save afrael/5671353 to your computer and use it in GitHub Desktop.
Grabs the content of a Windows Control and returns its bitmap image -- especially useful for long running operations where you might want to "dim" the control a put a spinner on top of it.
/*
GrabControlBitmapImage
Created on 2013/05/28 by Afrael Ortiz
Grabs the content of a Windows Control and returns its bitmap image
-- "do whatever you want with this" license --
*/
public static Bitmap GrabControlBitmapImage(Control control)
{
var controlBounds = control.Bounds;
using (var bitmap = new Bitmap(controlBounds.Width, controlBounds.Height))
{
using (var g = Graphics.FromImage(bitmap))
{
var controlLocation = control.PointToScreen(Point.Empty);
g.CopyFromScreen(controlLocation, Point.Empty, controlBounds.Size);
}
return new Bitmap(bitmap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment