locked
Thumbnail image of UserControl or Window RRS feed

  • 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.

     

    If you are using styles in a resourcestyledictionary for your project it gets a little tricky as you have to put all the resources in the same xaml markup instead of using a separate file. Also that snapshot feature works only for Pages (no Windows / UserControl). So you would have to put your xaml inside the Pages tag instead of UserControl. If you don't need the transparency Ctrl + PrntScreen is the easy lazy option. If your looking for perfection you would have to look into photoshop, illustrator etc.

     

    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.

     

    If you are using styles in a resourcestyledictionary for your project it gets a little tricky as you have to put all the resources in the same xaml markup instead of using a separate file. Also that snapshot feature works only for Pages (no Windows / UserControl). So you would have to put your xaml inside the Pages tag instead of UserControl. If you don't need the transparency Ctrl + PrntScreen is the easy lazy option. If your looking for perfection you would have to look into photoshop, illustrator etc.

     

    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.

     

    If you are using styles in a resourcestyledictionary for your project it gets a little tricky as you have to put all the resources in the same xaml markup instead of using a separate file. Also that snapshot feature works only for Pages (no Windows / UserControl). So you would have to put your xaml inside the Pages tag instead of UserControl. If you don't need the transparency Ctrl + PrntScreen is the easy lazy option. If your looking for perfection you would have to look into photoshop, illustrator etc.

     

    Ananth


    Agree!  I usually use the second method.
    Thursday, January 29, 2009 3:50 AM