I need help converting a Bitmap to a DIB IntPtr

j_t

Member
Joined
Jan 8, 2007
Messages
6
Programming Experience
10+
I need help converting a 1bpp windows Bitmap to a DIB IntPtr, but using the following code produces a DDB.

VB.NET:
     Dim bm As New Bitmap("C:\test.jpg")
     Dim hBitmap As System.IntPtr = bm.GetHbitmap
     ImgMod(hBitmap, 27) ' Third party Function looking for a IntPtr to a DIB

Currently the ImgMod function is complaining that the handle is a DDB instead of a DIB.

Any Ideas?
 
Can't remember where I got this from to credit the source.

VB.NET:
Imports System.IO
Imports System.Drawing.Imaging

Public Class DIB
    Dim ms As MemoryStream
    Dim bmpInfo As BITMAPINFO

    Friend Structure BITMAPINFOHEADER
        Dim biSize As UInteger
        Dim biWidth As Integer
        Dim biHeight As Integer
        Dim biPlanes As UShort
        Dim bitCount As UShort
        Dim biCompression As UInteger
        Dim biSizeImage As UInteger
        Dim biXPelsPerMeter As Integer
        Dim biYPelsPerMeter As Integer
        Dim biClrUsed As UInteger
        Dim biClrImportant As UInteger
    End Structure

    Friend Structure RGBQUAD
        Dim rgbBlue As Byte
        Dim rgbGreen As Byte
        Dim rgbRed As Byte
        Const rgbReserved As Byte = 0
    End Structure

    Friend Structure BITMAPINFO
        Dim bmiHeader As BITMAPINFOHEADER
        Dim bmiColors() As RGBQUAD
    End Structure

    Public Sub New(ByVal bmp As Bitmap)
        ms = New MemoryStream
        bmp.Save(ms, ImageFormat.Bmp)
        Dim b() As Byte = ms.ToArray
        Array.Copy(b, 14, b, 0, b.Length - 14)
        ms.Dispose()
        ms = New MemoryStream(b)
        bmpInfo.bmiHeader = GetBitmapInfoHeader()
        If bmpInfo.bmiHeader.bitCount < 9 Then
            Dim PalEntries As Integer = 1 << (bmpInfo.bmiHeader.bitCount - 1)
            ReDim bmpInfo.bmiColors(PalEntries - 1)
            Array.Copy(b, 14, bmpInfo.bmiColors, 0, PalEntries)
        End If
    End Sub

    Private Function GetBitmapInfoHeader() As BITMAPINFOHEADER
        Dim bih As BITMAPINFOHEADER
        Dim br As New BinaryReader(ms)

        With bih
            .biSize = br.ReadUInt32
            .biWidth = br.ReadInt32
            .biHeight = br.ReadInt32
            .biPlanes = br.ReadUInt16
            .bitCount = br.ReadUInt16
            .biCompression = br.ReadUInt32
            .biSizeImage = br.ReadUInt32
            .biXPelsPerMeter = br.ReadInt32
            .biYPelsPerMeter = br.ReadInt32
            .biClrUsed = br.ReadUInt32
            .biClrImportant = br.ReadUInt32
        End With

        Return bih
    End Function

End Class

VB.NET:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim bmp As New Bitmap("C:\Temp\bear.jpg")
        Dim dib As New DIB(bmp)

    End Sub
 
Thanks MattP !!!

I'll give it a go when I get back. I had move quickly onto another project. I ended up passing the Bitmap to a C++ DLL that passed back the handle to a DIB.
 
And then what?

This code means that we've converted a bitmap into an object called DIB. What do we have to do now to convert this DIB into a bitmap again, or stream or binary array. The purpose is to get the handle of the DIB image right?
 
Back
Top