Click here to Skip to main content
Licence 
First Posted 6 Nov 2006
Views 61,654
Bookmarked 38 times

DIB to System.Bitmap

By | 6 Nov 2006 | Article
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).

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

About the Author

bijulsoni

Software Developer
Microsoft
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiontnx PinmemberMojtaba Candovani7:57 5 Mar '12  
GeneralCreating Bitmap from DIB Pinmembera.stacchetti.ibed4:19 27 Apr '11  
GeneralGreat code, but doesn't work in WPF Pinmemberpatrickveenstra3:37 8 Aug '08  
GeneralRe: Great code, but doesn't work in WPF Pinmemberpatrickveenstra7:27 8 Aug '08  
GeneralRe: Great code, but doesn't work in WPF Pinmemberbijulsoni7:44 8 Aug '08  
GeneralRe: Great code, but doesn't work in WPF Pinmemberpatrickveenstra8:07 8 Aug '08  
GeneralRe: Great code, but doesn't work in WPF Pinmemberpatrickveenstra0:50 9 Aug '08  
GeneralCreating Bitmap from DIB without PInvoke (Code inside) PinmemberRenegade222:24 23 Jun '08  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pinmembersamorim14:31 4 Sep '08  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pinmemberbijulsoni20:55 4 Sep '08  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) [modified] Pinmemberoobayly12:13 18 Dec '08  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) PinmemberHPMV15:02 31 Jan '10  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pinmemberlytico14:56 3 Apr '09  
GeneralRe: Creating Bitmap from DIB without PInvoke (Code inside) Pinmembertamerqatar19:46 5 Nov '10  
GeneralAwesome code Pinmemberferchitu10:55 29 Apr '08  
Questionthanks but how to get the pointer to DIBpixels? Pinmemberduyong0610200221:12 14 Apr '08  
AnswerRe: thanks but how to get the pointer to DIBpixels? Pinmemberbijulsoni11:14 16 Apr '08  
AnswerRe: thanks but how to get the pointer to DIBpixels? Pinmemberq325mg12:57 4 Feb '10  
GeneralGdipCreateBitmapFromGdiDib return values Pinmemberrborn3:38 22 Dec '06  
GeneralRe: GdipCreateBitmapFromGdiDib return values PinmemberAmagaeruOtoko4:51 14 Sep '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 6 Nov 2006
Article Copyright 2006 by bijulsoni
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid