Convert from System.Drawing.Icon to System.Media.ImageSource and vice versa

with the transition from Winform to WPF, there is rising/upsurging demand to convert from System.Drawing.Icon to System.Media.ImageSource and vice versa (you cannot rule out the possibility to host new tech object in old ones)

 

Below shows how you can do to convert from System.Drwing.Icon to ImageSource.

 

 

 

internal static class IconUtilities
  {
    // you may refer to this page for details on how to convert from System.Drawing.Icon to System.Media.ImageSource
    // http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource
    [DllImport("gdi32.dll", SetLastError = true)]
    private static extern bool DeleteObject(IntPtr hObject);

    public static ImageSource ToImageSource(this Icon icon)
    {
      Bitmap bitmap = icon.ToBitmap();
      IntPtr hBitmap = bitmap.GetHbitmap();

      ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
          hBitmap,
          IntPtr.Zero,
          Int32Rect.Empty,
          BitmapSizeOptions.FromEmptyOptions());

      if (!DeleteObject(hBitmap))
      {
        throw new Win32Exception();
      }

      return wpfBitmap;
    }
  }
 

 

 

note: the referenced material, contains more solution to do the conversion:

 

 

 

withou losing fairness, here is the code that uses the MemoryStream,

 

 

 

public static ImageSource ToImageSourceWithMemoryStream(this Icon icon)
    {
      using (System.IO.MemoryStream iconstream = new System.IO.MemoryStream())
      {
        icon.Save(iconstream);
        iconstream.Seek(0, System.IO.SeekOrigin.Begin);
        return System.Windows.Media.Imaging.BitmapFrame.Create(iconstream);
      }

    }
 

 

 

 

And you may wonder how do we do the reverse, that is from System.Windows.Media.Imaging.ImageSource  or one of its descendant class, that is called System.Windows.Media.Imaging.BitmapSource to System.Drawing.Bitmap...

 

Here is one code, however, please be noted, the code has not been fully testified,  it may work as expected or it may not. 

 

 

internal static class BitmapSourceUtilities
  {

    public static Bitmap ToBitmap(this BitmapSource imageSource, double width, double height)
    {

      double newwidthratio = width / (double)imageSource.PixelWidth;
      // have no idea why the calculation is as such
      double newheightratio = width * (double)imageSource.PixelHeight / (double)imageSource.PixelHeight / (double) imageSource.PixelWidth;

      BitmapSource transformedBitmapSource = new TransformedBitmap(
        imageSource,
        new ScaleTransform(newwidthratio, newheightratio)
        );
      int bitmapwidth = transformedBitmapSource.PixelWidth;
      int bitmapheight = transformedBitmapSource.PixelHeight;
      int stride = bitmapwidth * (transformedBitmapSource.Format.BitsPerPixel + 7) / 8);

      byte[] bits = new byte[bitmapheight * stride];


      transformedBitmapSource.CopyPixels(bits, stride, 0);

      // you may want to put the following code that has the unsafe compiler turned on....
      unsafe
      {
        fixed (byte* pBits = bits)
        {
          IntPtr ptr = new IntPtr(pBits);
          System.Drawing.Bitmap bitmap = new Bitmap(
            bitmapwidth, 
            bitmapheight,
            stride, 
            System.Drawing.Imaging.PixelFormat.Format32bppArgb,
            ptr);
          return bitmap;
        }
      }
    }
  }
 

 

another note is that : The above code references the BitmapSource-Bitmap interop. you may find some boilerplat code that do grayscale and others..

 

 

 

note: synonym of rising..

 

 

climbing

Synonyms:

 

advancing, ascending, emerging, going up,growingincreasing, mounting, skyrocketing,soaring, spiraling, surging, upsurging
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值