title: Optimal way to resize and bind byte[] to image in wpf
body: <p>I have <code>byte[]</code> that store some image of unknown size. I need to resize this bitmap into 160 x 160 and bind to Wpf window.</p>
  
  <p>Should i convert <code>byte[]</code> to <code>Bitmap</code>, resize, make <code>BitmapSource</code> from it and then use in wpf? </p>
  
  <pre><code>//result of this method binds to wpf
  private static BitmapSource SetImage(byte[] dataBytes)
  {
          var picture = BitmapHelper.ByteToBitmap(dataBytes);
  
          var resizedPicture = BitmapHelper.Resize(picture, 160, 160);
  
          var bitmapSource = BitmapHelper.GetSourceFromBitmap(resizedPicture);
  
          bitmapSource.Freeze();
  
          return bitmapSource; 
  }
  </code></pre>
  
  <p>//BitmapHelper class:</p>
  
  <pre><code>    public static Bitmap ByteToBitmap(byte[] byteArray)
      {
          using (var ms = new MemoryStream(byteArray))
          {
              var img = (Bitmap)Image.FromStream(ms);
              return img;
          }
      }
  
      internal static Bitmap Resize(Bitmap bitmap, int width, int height)
      {
          var newImage = new Bitmap(width, height);
          using (var gr = Graphics.FromImage(newImage))
          {
              gr.SmoothingMode = SmoothingMode.HighQuality;
              gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
              gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
              gr.DrawImage(bitmap, new Rectangle(0, 0, width, height));
          }
  
          return newImage;
      }
  
  
      internal static BitmapSource GetSourceFromBitmap(Bitmap source)
      {
          Contract.Requires(source != null);
  
          var ip = source.GetHbitmap();
          BitmapSource bs;
          int result;
          try
          {
              bs = Imaging.CreateBitmapSourceFromHBitmap(ip,
                  IntPtr.Zero, Int32Rect.Empty,
                  BitmapSizeOptions.FromEmptyOptions());
          }
          finally
          {
              result = NativeMethods.DeleteObject(ip);
          }
          if (result == 0)
              throw new InvalidOperationException("NativeMethods.DeleteObject returns 0 (operation failed)");
  
          return bs;
      }
  
      internal static class NativeMethods
      {
         [DllImport("gdi32")]
         static internal extern int DeleteObject(IntPtr o);
      }
  </code></pre>
  
  <p>What is the fastest and common method for that case?</p>