How to use System.Drawing.Bitmap (HBitmap) in WPF

6 במאי 2007

תגיות: , ,
5 תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is]

Actually, the question is: "How to use nice feature of Resource code generation, presented in VS2005 within WPF". Well, that's not really efficient way, but whatever. As you, probably, know in WinForms (.NET2.0) and Visual Studio 2005 there is code generator for system resources. It makes us able to have intellisense support of VS development environment for loading resources from the application.

Actually, the code generator creates a butch of static properties, that gets objects from application ResourceManager. If you'll take a look into Reesources.Designer.cs file, you'll find there internal static readonly properties like this

internal static System.Drawing.Bitmap p1060981 {
get {
object obj = ResourceManager.GetObject("p1060981", resourceCulture);
return ((System.Drawing.Bitmap)(obj));


How to get this and load my images (or any other resources) and use then in WPF this way?

image.Source = ResourceHelper1.Properties.Resources.p1060981;


Well, you can not and the main reason is, that it returns System.Drawing.Bitmap, which actually, HBitmap and can not be used directly as image source for WPF. But if you really want try this small helper

public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,


It gets System.Drawing.Bitmap (from WindowsBased) and converts it into BitmapSource, which can be actually used as image source for your Image control in WPF.

This is not very efficient way. It's much better to use Resources management of WPF as described here, but in some cases (this one is not reason 🙂 ) you have to use Forms ResourceManager. One of them, is if you need pixel level access to your bitmap.

Source code for this article

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

5 תגובות

  1. JuanCamilo7 בפברואר 2008 ב 0:45

    dude, you rock. it is awesome, specially if your using file stream type of the new sql.
    take care.

  2. Tolgahan Albayrak10 במרץ 2008 ב 4:26

    Sometimes (when you need to re-create some bitmaps or change them) it will crash the memory.. This is a small dix for your sollution..
    getHBitmap will allocate memory by total image size.. And each call will allocate again.. So if you delete old hBitmap after creating BitmapSource, I think, the RAM will feel better 🙂

    static extern int DeleteObject(IntPtr o);

    public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
    IntPtr ip = source.GetHbitmap();
    BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
    return bs;


  3. Part Time Australian24 ביוני 2008 ב 1:11

    Why not make them extension methods…

    public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)

  4. Tamir Khason24 ביוני 2008 ב 11:37

    Part Time Australian, This article has been written before attached methods were invented