1

Is there any way to convert from a System.Windows.Media.Imaging.BitmapSource to a System.Windows.Media.Visual? I have an instance of a BitmapSource but I'd like to convert it to a Visual so I can take advantage of WPF vector graphics. I've seen lots of examples of going from Visual to bitmap but how can I go the other direction using C#?

var stream = new FileStream(
    @"C:\Image.png", FileMode.Open, FileAccess.Read, FileShare.Read);

var decoder = new PngBitmapDecoder(
    stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

BitmapSource bitmapSource = decoder.Frames[0];
// Visual visual = bitmapSource.ToVisual(); <-- I'd like to do this
mihai
  • 4,592
  • 3
  • 29
  • 42
Robin
  • 2,278
  • 3
  • 26
  • 46
  • 3
    You could put the BitmapSource in an Image control, which is a Visual. However, it would still be a raster image. – Clemens Nov 06 '14 at 19:41
  • 2
    Please be more specific. As Clemens notes, the `Image` control _is_ a `Visual`. Alternatively, you could create a `DrawingVisual` and render the bitmap into that. But either way, you're still going to have a bitmap underneath. What is it about either of those two approaches that doesn't work in your case? – Peter Duniho Nov 06 '14 at 19:43
  • I'm really looking for a way to have a vector based image underneath. How can I convert a raster image to a vector image? – Robin Nov 07 '14 at 00:42
  • @Robin: that's a completely different question. The short answer given the precise wording of the question is, you can't. WPF doesn't have a "bitmap-to-vector" feature. You'd need to write your own or use some third-party library. A web search will show you plenty of options for either. – Peter Duniho Nov 07 '14 at 04:51

0 Answers0