title: Image does not show up in Wpf Image Control
body: <p>I'm re-writing a app of mine, where I create a barcode image using the <code>Barcode Image Generation Libary</code> by Brad Barnhill (<a href="http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library" rel="nofollow">http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library</a>). </p>
  
  <p>In this article everything is explaned how to do it in Windows Forms. But now - using Wpf - there are some errors. E.g.: The result of the function <code>Encode</code>  returns a <code>System.Drawing.Image</code> but when I want to display this Image in a <code>Wpf Image Control</code> the <code>Source</code> property wants a <code>System.Windows.Media.ImageSource</code>.</p>
  
  <p>So I did some reserach of how to convert a <code>Drawing.Image</code> in a <code>Media.ImageSource</code>. I found some snippets but they don't work as expected.</p>
  
  <p>Currently I use this code:</p>
  
  <pre><code>// Import:
  using Media = System.Windows.Media;
  using Forms = System.Windows.Forms;
  
  
  // Setting some porperties of the barcode-object
  this.barcode.RotateFlipType = this.bcvm.Rotation.Rotation;
  this.barcode.Alignment = this.bcvm.Ausrichtung.Alignment;
  this.barcode.LabelPosition = this.bcvm.Position.Position;
  
  // this.bcvm is my BarcodeViewModel for MVVM
  var img = this.barcode.Encode(
      this.bcvm.Encoding.Encoding, 
      this.bcvm.EingabeWert, 
      this.bcvm.ForeColor.ToDrawingColor(), 
      this.bcvm.BackColor.ToDrawingColor(), 
      (int)this.bcvm.Breite, 
      (int)this.bcvm.Hoehe
  );
  
  this.imgBarcode.Source = img.DrawingImageToWpfImage();
  
  this.imgBarcode.Width = img.Width;
  this.imgBarcode.Height = img.Height;
  
  // My conversion methode. It takes a Drawing.Image and returns a Media.ImageSource
  public static Media.ImageSource ToImageSource(this Drawing.Image drawingImage)
  {
      Media.ImageSource imgSrc = new Media.Imaging.BitmapImage();
      using (MemoryStream ms = new MemoryStream())
      {
          drawingImage.Save(ms, Drawing.Imaging.ImageFormat.Png);
  
          (imgSrc as Media.Imaging.BitmapImage).BeginInit();
          (imgSrc as Media.Imaging.BitmapImage).StreamSource = new MemoryStream(ms.ToArray());
          (imgSrc as Media.Imaging.BitmapImage).EndInit();
      }
      return imgSrc;
  }
  </code></pre>
  
  <p>When running this code an converting the image (and assigning it to the Image control) there is nothing displayed</p>