Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,

In WPF, (.Net Framework 3.5) I want to convert Bitmap to ImageSource. I've googled yesterday but I didn't find any solution that works in Framework 3.5

The Bitmap is: System.Drawing.Bitmap
Posted
Updated 13-Apr-16 6:30am
v2

Hi try to read this link, it may help you :)

http://www.shujaat.net/2010/08/wpf-images-from-project-resource.html[^]
 
Share this answer
 
Comments
mariazingzing 17-Jul-13 2:18am    
It helps
Thank you
berrymaria 17-Jul-13 2:34am    
great :)
Here's the ValueConverter I wrote years ago.
It converts a Bitmap to ImageSource (specifically a BitmapFrame):
C#
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
 
namespace ResourcesLibrary
{
  [ValueConversion(typeof(Bitmap), typeof(ImageSource))]
  public class BitmapToImageSourceConverter : IValueConverter
  {
    [DllImport("gdi32.dll")]
    private static extern bool DeleteObject(IntPtr hObject);
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      Bitmap bmp = value as Bitmap;
      if (bmp == null)
        return null;
      using (Stream str = new MemoryStream())
      {
        bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
        str.Seek(0, SeekOrigin.Begin);
        BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        return bdc.Frames[0];
      }
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}
 
Share this answer
 
v2
Comments
Richard Deeming 13-Apr-16 13:13pm    
Did you notice the date on the question?
Matt T Heffron 13-Apr-16 13:20pm    
Rats!
I did notice that two other Solutions were "recent", so I didn't look at the original date.
FYI: That method does not support 32bbp bitmaps with alpha channel.

Try this:

C#
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
      source.GetHbitmap(),
      IntPtr.Zero,
      Int32Rect.Empty,
      BitmapSizeOptions.FromEmptyOptions());
 
Share this answer
 
Comments
CHill60 9-Apr-15 16:55pm    
Which method are you referring to? It may have been better to use the "Have a Question or Comment?" link next to the relevant post
tlhIn`toq 10-Apr-15 8:11am    
Considering solution 1 isn't really a solution because it throws an exception and has been commented as such, common sense should tell ya that I was referring to solution 2. Since I was offering a new solution with code that does support 32bbp with alpha channel it seemed right to add it as a solution.
Member 14881992 20-Aug-21 4:05am    
Thanks
this is a good approach for .png images
try this..:)

C#
public ImageSource imageSourceForImageControl
{
 get
 {
 ImageSourceConverter c = new ImageSourceConverter();
 return (ImageSource)c.ConvertFrom(yourBitmap);
 }
}
 
Share this answer
 
Comments
mariazingzing 17-Jul-13 1:24am    
Thank you but exception:
ImageSourceConverter cannot convert from System.Drawing.Bitmap
Try This:

C#
Image hImage = new Image();
            hImage.Source = new BitmapImage(new Uri(@"file:///path\ImageName.png"));/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900