Click here to Skip to main content
15,308,835 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
Updated 13-Apr-16 6:30am

Hi try to read this link, it may help you :)[^]
mariazingzing 17-Jul-13 2:18am
It helps
Thank you
chimcham 17-Jul-13 2:34am
great :)
Here's the ValueConverter I wrote years ago.
It converts a Bitmap to ImageSource (specifically a BitmapFrame):
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
    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();
Richard Deeming 13-Apr-16 13:13pm
Did you notice the date on the question?
Matt T Heffron 13-Apr-16 13:20pm
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:

return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
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
this is a good approach for .png images
try this..:)

public ImageSource imageSourceForImageControl
 ImageSourceConverter c = new ImageSourceConverter();
 return (ImageSource)c.ConvertFrom(yourBitmap);
mariazingzing 17-Jul-13 1:24am
Thank you but exception:
ImageSourceConverter cannot convert from System.Drawing.Bitmap
Try This:

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

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