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