Question on CreateDIBSection under VB .Net

Question on CreateDIBSection under VB .Net

Post by aimle.. » Sun, 09 Jun 2002 07:59:34

I'm writing a function to save data in an ascii text file  to a tiff image.
I started out using GDI  and a third party library to prove the concept,
then started looking at GDI+ for the native tif support.  I can generate the
image, although when the image is viewed in our document imaging system at
full page size the quality is poor.  The quality of the image remains poor
until you've zoomed in on it about 50%.  I did a little more investigation
and found that my tiff files are using 96 dpi and was able to track that
back to my CreateCompatibleDC(0) and CreateCompatibleBitmap statements.
Most of the images in our document imaging system are scanned images at
either 200 or 300 dpi.  I'm assuming this difference explains the poor
quality of my documents as compared to the scanned ones at the same zoom

All my bitmaps are being created at this DPI (96 is the DPI of the display
device as reported by GetDeviceCaps), no matter what I try.  Upon doing a
little more investigation I stumbled  onto the CreateDIBSection api call.
Looking at the BITMAPINFO structure I noticed that I am able to specify the
X and Y dpi of the created bitmap.  This seems exactly like what I want.
The problem is tha I cannot call it from Visual Basic .Net.  I can take
nearly the same code to VB 6 and it works fine, but fails on .Net with a
"Object Reference Not set" exception (or something close to).

Here is My declare statement for CreateDIBSection (taken from AllAPI and
converting all Longs to Integers):

Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Integer, ByVal
pBitmapInfo As BITMAPINFO, ByVal un As Integer, ByVal lplpVoid As Integer,
ByVal handle As Integer, ByVal dw As Integer) As Integer

Also Here are my struct definitions:

    ' Bitmap Header Definition

    Public Structure BITMAP             '14 bytes
        Public bmType As Integer
        Public bmWidth As Integer
        Public bmHeight As Integer
        Public bmWidthBytes As Integer
        Public bmPlanes As Short
        Public bmBitsPixel As Short
        Public bmBits As Integer
    End Structure

    Public Structure RGBQUAD
        Public rgbBlue As Byte
        Public rgbGreen As Byte
        Public rgbRed As Byte
        Public rgbReserved As Byte
    End Structure

    ' structures for defining DIBs

    Public Structure BITMAPINFOHEADER '40 bytes
        Public biSize As Integer
        Public biWidth As Integer
        Public biHeight As Integer
        Public biPlanes As Short
        Public biBitCount As Integer
        Public biCompression As Integer
        Public biSizeImage As Integer
        Public biXPel*eter As Integer
        Public biYPel*eter As Integer
        Public biClrUsed As Short
        Public biClrImportant As Integer
    End Structure

 Public Structure BITMAPINFO
        Public bmiHeader As BITMAPINFOHEADER
        Public bmiColors As RGBQUAD
    End Structure

Finally, here's the simple piece of code I'm  using to try to play with the
function call the fiunction that fails (copy/paste essentially the same in
vb 6 and it will work):

          Dim bi24BitInfo As BITMAPINFO

        With bi24BitInfo.bmiHeader
            .biBitCount = 24
            .biCompression = BI_RGB
            .biPlanes = 1
            .biSize = Len(bi24BitInfo.bmiHeader)
            .biWidth = 100
            .biHeight = 100
        End With

        Dim iDC As Integer = CreateCompatibleDC(0)
        Dim iBitmap As Integer = CreateDIBSection(iDC, bi24BitInfo,
       DIB_RGB_COLORS, 0, 0, 0)
        SelectObject(iDC, iBitmap)


Can anyone see something that I might be missing here that will not allow me
to call this function???  All I want to do is to give my code an HBITMAP
thats not 96x96 dpi but more like 200x200 or optimisticly 300x300.

Thanks much,

Barry Lance


Question on CreateDIBSection under VB .Net

Post by Mike D Sutto » Sun, 09 Jun 2002 08:10:01

Quote:> I'm writing a function to save data in an ascii text file  to a tiff
> I started out using GDI  and a third party library to prove the concept,
> then started looking at GDI+ for the native tif support.

<snip, snip>

You'd be better off asking this question in a VB.NET newsgroup rather than


 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://


1. CreateDIBSection in VB

How about some specifics on how it fails to work?  I've never used
CreateDIBSection() (just looked it up and it seems pretty neat).  Do you
have Dan Appleman's book (VB 5 Programmer's Guide to the Win32 API)?  It's
actually got a sample using this function.  If you're a C programmer, you
may be just missing a VB-specific detail (such as proper use of ByVal or
referring to an array by its first element).  Post some code.

Jim Deutch

2. HTML for random access of GIFs?


4. Screen capture

5. Another question on CreateDIBSection with Mono DIBs...

6. alpha channel and transparent GIFs

7. wglUseFontOutlines & VB.NET

8. Problems with Viewing Corel Draw 8 Thumbnails

9. OpenGL & VB.NET

10. VB.NET, cards.dll, PictureBox

11. APIs and

12. bitblt in

13. update for SafeArray2D + CopyMemory trick