locked
How to i convert 'System.Windows.Controls.Image' to 'System.Drawing.Image'. RRS feed

  • Question

  • Hi Experts,

    I tried to convert image from 'System.Windows.Controls.Image' to 'System.Drawing.Image'. using the following code.

    ImageConverter converter = new ImageConverter();
    var imge = (System.Drawing.Image)converter.ConvertTo(image, typeof(System.Drawing.Image));

    but am getting the following error.

    'ImageConverter' is unable to convert 'System.Windows.Controls.Image' to 'System.Drawing.Image'.

    Please help me anybody.


    -Ramesh

    Wednesday, July 4, 2012 11:07 AM

Answers

  • private System.Windows.Controls.Image ConvertDrawingImageToWPFImage(System.Drawing.Image gdiImg)
    {
    
    
    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
    
    //convert System.Drawing.Image to WPF image
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(gdiImg);
    IntPtr hBitmap = bmp.GetHbitmap();
    System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    
    img.Source = WpfBitmap;
    img.Width = 500;
    img.Height = 600;
    img.Stretch = System.Windows.Media.Stretch.Fill;
    return img;
    }


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nairs

    Wednesday, July 4, 2012 11:22 AM

All replies

  • http://smisro.blogspot.com/2006/05/using-imagelist-converting.html

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, July 4, 2012 11:20 AM
  • private System.Windows.Controls.Image ConvertDrawingImageToWPFImage(System.Drawing.Image gdiImg)
    {
    
    
    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
    
    //convert System.Drawing.Image to WPF image
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(gdiImg);
    IntPtr hBitmap = bmp.GetHbitmap();
    System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    
    img.Source = WpfBitmap;
    img.Width = 500;
    img.Height = 600;
    img.Stretch = System.Windows.Media.Stretch.Fill;
    return img;
    }


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nairs

    Wednesday, July 4, 2012 11:22 AM
  • This question is not answered at all. It makes the reverse conversion.

    Anybody that knows the right answer?

    Wednesday, March 4, 2015 2:13 PM