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,growingincreasingmounting, skyrocketing,soaring, spiraling, surging, upsurging

你可能感兴趣的:(WPF)