Answered by:
Thumbnail image of UserControl or Window

Question
-
In other words, what is the best way to capture an image of the contents of a UserControl (with other controls on it) or a Window?
Thank you.
Tuesday, January 27, 2009 2:22 PM
Answers
-
If you want to save to disk you can use RenderTargetBitmap like this:
//export to image //TODO: add error checking private bool exportToImage(string path, Visual v) { RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)v.Width, (int)v.Height, 96d, 96d, PixelFormats.Pbgra32); targetBitmap.Render(v); // add the RenderTargetBitmap to a Bitmapencoder //BmpBitmapEncoder encoder = new BmpBitmapEncoder(); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(targetBitmap)); // save file to disk using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.OpenOrCreate)) { encoder.Save(fs); } return true; }
If what you want to do is actually embed the thumbnail image somewhere else in your application, you can try something like this using VisualBrush.- Proposed as answer by joeAtBluebeam Tuesday, January 27, 2009 10:04 PM
- Marked as answer by Tao Liang Thursday, January 29, 2009 3:48 AM
Tuesday, January 27, 2009 10:04 PM -
You have a tool called Kaxaml (a lightweight advanced xaml editor like xamlpad) that allows you to create png images from your xaml markup while maintaining transparency.
Ananth
- Marked as answer by Tao Liang Thursday, January 29, 2009 3:48 AM
Tuesday, January 27, 2009 3:56 PM
All replies
-
You have a tool called Kaxaml (a lightweight advanced xaml editor like xamlpad) that allows you to create png images from your xaml markup while maintaining transparency.
Ananth
- Marked as answer by Tao Liang Thursday, January 29, 2009 3:48 AM
Tuesday, January 27, 2009 3:56 PM -
I meant to do this from software code.
I know of one way using VisualBrush but that seems too complex.
Thank you
Tuesday, January 27, 2009 5:11 PM -
If you want to save to disk you can use RenderTargetBitmap like this:
//export to image //TODO: add error checking private bool exportToImage(string path, Visual v) { RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)v.Width, (int)v.Height, 96d, 96d, PixelFormats.Pbgra32); targetBitmap.Render(v); // add the RenderTargetBitmap to a Bitmapencoder //BmpBitmapEncoder encoder = new BmpBitmapEncoder(); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(targetBitmap)); // save file to disk using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.OpenOrCreate)) { encoder.Save(fs); } return true; }
If what you want to do is actually embed the thumbnail image somewhere else in your application, you can try something like this using VisualBrush.- Proposed as answer by joeAtBluebeam Tuesday, January 27, 2009 10:04 PM
- Marked as answer by Tao Liang Thursday, January 29, 2009 3:48 AM
Tuesday, January 27, 2009 10:04 PM -
joeAtBluebeam's code is great!Thursday, January 29, 2009 3:49 AM
-
Ananth878 said:
You have a tool called Kaxaml (a lightweight advanced xaml editor like xamlpad) that allows you to create png images from your xaml markup while maintaining transparency.
Ananth
Agree! I usually use the second method.Thursday, January 29, 2009 3:50 AM