Click here to Skip to main content
15,867,835 members
Articles / Multimedia / GDI+
Article

DIB to System.Bitmap

Rate me:
Please Sign up or sign in to vote.
4.95/5 (17 votes)
6 Nov 2006 133K   41   26
An article on DIB to Bitmap conversion.

Introduction

This article describes a way to retrieve an image from a DIB handle to a System.Bitmap object.

I am developing a TWAIN scanning solution for my enterprise. I have developed code for scanning an image. The only problem  was that after an image is scanned, the scanned image is available in memory in DIB format and all I am having is an IntPtr pointing to the DIB image.

My purpose is to retrieve the scanned image from the DIB handle and show it in a picture box. For that, I need to convert the DIB handle into a Bitmap object. The following code shows how I solved this problem.

Using the code

Here is a code for the function BitmapFromDIB which will return you a Bitmap object from the DIB handle.

This function accepts two parameters

  • pDIB: it is a pointer to a DIB which is returned after scanning an image
  • pPix: it is a pointer to DIBpixels

Here, we have used the function GdipCreateBitmapFromGdiDib exported from Gdiplus.dll to get a pointer to a Bitmap. We then use reflection, and pass this pointer to the static internal method Bitmap.FromGDIplus(IntPtr pGdiBitmap).

C#
using System.Drawing; 
using System.Reflection; // . . . 

[DllImport("GdiPlus.dll", 
   CharSet=CharSet.Unicode, ExactSpelling=true)] 
private static extern int GdipCreateBitmapFromGdiDib(IntPtr pBIH, 
                          IntPtr pPix, out IntPtr pBitmap); 


public static Bitmap BitmapFromDIB(IntPtr pDIB,IntPtr pPix) 
{ 


    MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus", 
                    BindingFlags.Static | BindingFlags.NonPublic); 

    if (mi == null) 
        return null; // (permission problem) 

    IntPtr pBmp = IntPtr.Zero; 
    int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp); 

    if ((status == 0) && (pBmp != IntPtr.Zero)) // success 
        return (Bitmap)mi.Invoke(null, new object[] {pBmp}); 

    else 
        return null; // failure 
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Microsoft
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPointer Pin
stixoffire6-Nov-14 9:23
stixoffire6-Nov-14 9:23 
GeneralGreat article Pin
Lee.21-Jul-13 15:48
Lee.21-Jul-13 15:48 
Questionthank you Pin
kevingif3-Mar-13 23:05
kevingif3-Mar-13 23:05 
QuestionAssume this is public domain or is there any license? Pin
sbarnes18-Sep-12 9:06
sbarnes18-Sep-12 9:06 
QuestionHow to convert a bitmap to dib handle ? Pin
vbilici26-Jun-12 3:54
vbilici26-Jun-12 3:54 
Questiontnx Pin
Mojtaba Candovani5-Mar-12 7:57
Mojtaba Candovani5-Mar-12 7:57 
GeneralCreating Bitmap from DIB Pin
a.stacchetti.ibed27-Apr-11 4:19
a.stacchetti.ibed27-Apr-11 4:19 
GeneralGreat code, but doesn't work in WPF Pin
patrickveenstra8-Aug-08 3:37
patrickveenstra8-Aug-08 3:37 
I've tried your code in .NET 3.5 and it works great, but now I've loaded the class in WPF (adding a reference to System.Drawing) and it doesn't work.

method: BitmapFromDIB
line: int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);

returns: status = 18 and pBmp = 0

Any idea what in WPF can cause this code not to work?

Thanks in advance!
GeneralRe: Great code, but doesn't work in WPF Pin
patrickveenstra8-Aug-08 7:27
patrickveenstra8-Aug-08 7:27 
GeneralRe: Great code, but doesn't work in WPF Pin
bijulsoni8-Aug-08 7:44
bijulsoni8-Aug-08 7:44 
GeneralRe: Great code, but doesn't work in WPF Pin
patrickveenstra8-Aug-08 8:07
patrickveenstra8-Aug-08 8:07 
GeneralRe: Great code, but doesn't work in WPF Pin
patrickveenstra9-Aug-08 0:50
patrickveenstra9-Aug-08 0:50 
GeneralCreating Bitmap from DIB without PInvoke (Code inside) Pin
Renegade223-Jun-08 22:24
Renegade223-Jun-08 22:24 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pin
SergioAmorim4-Sep-08 14:31
SergioAmorim4-Sep-08 14:31 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pin
bijulsoni4-Sep-08 20:55
bijulsoni4-Sep-08 20:55 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) [modified] Pin
oobayly18-Dec-08 12:13
oobayly18-Dec-08 12:13 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pin
Robin Cheng (HPMV)31-Jan-10 15:02
Robin Cheng (HPMV)31-Jan-10 15:02 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pin
lytico3-Apr-09 14:56
lytico3-Apr-09 14:56 
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pin
Tamer Hatoum5-Nov-10 19:46
Tamer Hatoum5-Nov-10 19:46 
GeneralAwesome code Pin
ferchitu29-Apr-08 10:55
ferchitu29-Apr-08 10:55 
Questionthanks but how to get the pointer to DIBpixels? Pin
duyong0610200214-Apr-08 21:12
duyong0610200214-Apr-08 21:12 
AnswerRe: thanks but how to get the pointer to DIBpixels? Pin
bijulsoni16-Apr-08 11:14
bijulsoni16-Apr-08 11:14 
AnswerRe: thanks but how to get the pointer to DIBpixels? Pin
q325mg4-Feb-10 12:57
q325mg4-Feb-10 12:57 
GeneralGdipCreateBitmapFromGdiDib return values Pin
rborn22-Dec-06 3:38
rborn22-Dec-06 3:38 
GeneralRe: GdipCreateBitmapFromGdiDib return values Pin
TODA Kageyu14-Sep-07 4:51
TODA Kageyu14-Sep-07 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.