08 October 2014

WPF - Use a System.Drawing.Image as ImageSource

I had a System.Drawing.Image thumbnail that I wanted to use as the ImageSource of an Image element in WPF. The following code – compiled with some help – did the trick:
using draw = System.Drawing;
...
draw.Image thumb = Browser.WebView.Capture(false); //The original image.

using (MemoryStream stream = new MemoryStream()) {
  thumb.Save(stream, draw.Imaging.ImageFormat.Bmp);
  stream.Position = 0;
  stream.Seek(0, SeekOrigin.Begin);
  BitmapImage bi = new BitmapImage();
  bi.BeginInit();
  bi.StreamSource = stream;
  bi.CacheOption = BitmapCacheOption.OnLoad;
  bi.EndInit();
  Thumbnail.Source = bi; //The image element.
  thumb.Dispose();
}

No comments:

Post a Comment