Navigation


Markus on Development and Publishing

This is Markus Egger's professional blog, which covers topics such as development, publishing, and business in general. As the publisher of CoDe and CoDe Focus magazines, and as the President and Chief Software Architect of EPS Software Corp., Markus shares his insights and opinions on this blog.

Content Area Footer

Friday, June 27, 2008
Converting GDI+ Images to WPF Bitmap Sources

Here is something I have shown as a little side-note at various conferences, and I have people sending me email about how I did it: Basically, this is about converting GDI+ Bitmap/Image objects to something WPF can consume. The trick is to convert the Image into BitmapSource objects, which can then be used on all kinds of WPF objects, such as images or brushes. Here is how to convert GDI+ Bitmaps into WPF BitmapSource objects:

Bitmap bmp = some bitmap...; // Replace with a real bitmap
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
    bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
    BitmapSizeOptions.FromEmptyOptions());

Sometimes, you may want to bind directly to a GDI+ bitmap object. In that case, you can create a ValueConverter object that does the conversion for you:

public class BitmapConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Converts a GDI bitmap to an image source
        Bitmap bmp = value as Bitmap;
        if (bmp != null)
        {
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                bmp.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            return bitmapSource;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

That's it! Not much to it...



Posted @ 2:28 PM by Egger, Markus (markus@code-magazine.com)


 

 

 

 

 

 

 



My Twitter Status


    follow me on Twitter  


    Geo Caching
    Profile for MarkusEgger

    Syndication ng> RSS 2.0 RSS 2.0

    All My Blogs:
    My personal blogs:
    Dev and Publishing Dev and Publishing
    Travel and Internat. Living Travel and Internat. Living
    Other blogs I contribute to:
    Milos Blog (US) Milos Blog (US)
    VFPConv. Dev Blog (US) VFPConv. Dev Blog (US)
    VFPConv. Dev Blog (DE) VFPConv. Dev Blog (DE)

     

    Blog Archives
    All Blog Posts

    2012
        September (1)
        April (1)
        March (1)
    2011
        October (1)
        June (3)
        May (1)
        March (2)
        February (2)
        January (2)
    2010
        December (3)
        November (2)
        October (2)
        September (1)
        August (2)
        July (1)
        June (1)
        April (3)
        March (1)
        February (5)
        January (1)
    2009
        October (4)
        September (2)
        August (1)
        July (1)
        May (4)
        April (6)
        February (1)
        January (1)
    2008
        December (3)
        November (11)
        October (8)
        September (1)
        July (1)
        June (3)
        May (3)
        April (6)
        March (6)
        February (4)
    2007
        December (1)
        November (1)
        October (5)
        September (1)
        August (1)
        July (6)
        June (3)
        May (3)
        April (1)
        March (2)
        January (2)
    2006
        December (3)
        November (4)
        October (1)
        September (2)
        August (2)
        July (4)
        June (1)
        May (2)
        April (10)
        March (2)
        February (3)
        January (1)
    2005
        December (6)
        November (7)
        October (6)
        September (8)
        August (10)
        July (6)
        June (9)

     

     

     

    This Blog is powered by MilosTM Collaboration Components.