3

I have created a WPF UserControl library. And I would like to put a vector picture of each UserControl (in default settings) to documentation. So my question is: How do I create a vector image of a UserControl?

Thanks for any efforts.

David
  • 15,894
  • 22
  • 55
  • 66
Nikolas Jíša
  • 699
  • 1
  • 11
  • 29
  • 1
    Suggestions here might help http://stackoverflow.com/questions/10160740/how-to-render-a-visualelement-to-a-vector-based-image – keyboardP Apr 24 '13 at 00:38
  • Thanks for the suggestion, it doesn't seem like a bad option, however I've been rather wondering if there isn't a more elegant way to achieve this directly from XAML (and code behind). However unless someone brings a better way I'll use the Potrace program as you suggested. – Nikolas Jíša Apr 24 '13 at 01:00

1 Answers1

2

Capture a vector image seems difficult, and is you control composed of vector graphics only? If you just wish to get a png or bmp, below code might help:

ImageCapturer.SaveToPng(AnyControl, "Capture.png");

class ImageCapturer
{               
    public static void SaveToBmp(FrameworkElement visual, string fileName)
    {
        var encoder = new BmpBitmapEncoder();
        SaveUsingEncoder(visual, fileName, encoder);
    }

    public static void SaveToPng(FrameworkElement visual, string fileName)
    {
        var encoder = new PngBitmapEncoder();
        SaveUsingEncoder(visual, fileName, encoder);
    }

    private static void SaveUsingEncoder(FrameworkElement visual, 
                 string fileName, BitmapEncoder encoder)
    {
        RenderTargetBitmap bitmap = new RenderTargetBitmap(
               (int)visual.ActualWidth, 
               (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
        bitmap.Render(visual);
        BitmapFrame frame = BitmapFrame.Create(bitmap);
        encoder.Frames.Add(frame);

        using (var stream = File.Create(fileName))
        {
            encoder.Save(stream);
        }
    }
}
David
  • 15,894
  • 22
  • 55
  • 66