Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
ChangeToDib Method
See Also  Example
Leadtools Namespace > RasterImage Class : ChangeToDib Method



type
Type of DIB to create. Possible values are:
ValueMeaning
RasterConvertToDibType.BitmapInfoHeaderDIB that uses a BITMAPINFOHEADER
RasterConvertToDibType.BitmapV4HeaderDIB that uses a BITMAPV4HEADER. (Introduced in Windows 95 and Windows NT 4.0)
RasterConvertToDibType.BitmapV5HeaderDIB that uses a BITMAPV5HEADER (Introduced in Windows 2000 and Windows 98)
Changes a LEAD RasterImage object to a Windows Device Independent Bitmap (DIB).

Syntax

Visual Basic (Declaration) 
Public Function ChangeToDib( _
   ByVal type As RasterConvertToDibType _
) As IntPtr
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim type As RasterConvertToDibType
Dim value As IntPtr
 
value = instance.ChangeToDib(type)
C# 
public IntPtr ChangeToDib( 
   RasterConvertToDibType type
)
C++/CLI 
public:
IntPtr ChangeToDib( 
   RasterConvertToDibType type
) 

Parameters

type
Type of DIB to create. Possible values are:
ValueMeaning
RasterConvertToDibType.BitmapInfoHeaderDIB that uses a BITMAPINFOHEADER
RasterConvertToDibType.BitmapV4HeaderDIB that uses a BITMAPV4HEADER. (Introduced in Windows 95 and Windows NT 4.0)
RasterConvertToDibType.BitmapV5HeaderDIB that uses a BITMAPV5HEADER (Introduced in Windows 2000 and Windows 98)

Return Value

A handle to the device independent bitmap (DIB) this method creates.

Example

This example loads a RasterImage, changes it to a V5 DIB and writes out the DIB as a bitmap file.

Visual BasicCopy Code
<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalSize(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalLock(ByVal hMem As IntPtr) As IntPtr
End Function

<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalUnlock(ByVal hMem As IntPtr) As Integer
End Function

<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalFree(ByVal hMem As IntPtr) As IntPtr
End Function

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class BITMAPFILEHEADER
   Public bfType As UShort
   Public bfSize As UInteger
   Public bfReserved1 As UShort
   Public bfReserved2 As UShort
   Public bfOffBits As UInteger
End Class

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class BITMAPINFOHEADER
   Public biSize As UInteger
   Public biWidth As Integer
   Public biHeight As Integer
   Public biPlanes As UShort
   Public biBitCount As UShort
   Public biCompression As UInteger
   Public biSizeImage As UInteger
   Public biXPelsPerMeter As Integer
   Public biYPelsPerMeter As Integer
   Public biClrUsed As UInteger
   Public biClrImportant As UInteger
End Class

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class RGBQUAD
   Public rgbBlue As Byte
   Public rgbGreen As Byte
   Public rgbRed As Byte
   Public rgbReserved As Byte
End Class


Public Sub ChangeToDibExample()
   RasterCodecs.Startup()
   Dim codecs As New RasterCodecs()

   ' Load an image
   Dim image As RasterImage = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp", 24, CodecsLoadByteOrder.BgrOrGray, 1, 1)

   ' Change to DIB
   Dim hdib As IntPtr = image.ChangeToDib(RasterConvertToDibType.BitmapInfoHeader)

   ' Dispose the image since it is unusable now
   image.Dispose()

   ' Clean up
   codecs.Dispose()
   RasterCodecs.Shutdown()

   ' Save this DIB to disk as a BMP file

   Dim dibSize As Integer = GlobalSize(hdib).ToInt32()

   Dim bmfh As New BITMAPFILEHEADER()
   bmfh.bfType = &H4D42 ' "BM"
   bmfh.bfSize = CUInt(Marshal.SizeOf(GetType(BITMAPFILEHEADER)) + dibSize)
   bmfh.bfReserved1 = 0
   bmfh.bfReserved2 = 0

   Dim pdib As IntPtr = GlobalLock(hdib)

   Dim bmih As New BITMAPINFOHEADER()
   Marshal.PtrToStructure(pdib, bmih)

   bmfh.bfOffBits = CUInt(Marshal.SizeOf(GetType(BITMAPFILEHEADER)) + bmih.biSize + bmih.biClrUsed * Marshal.SizeOf(GetType(RGBQUAD)))

   Using fs As FileStream = File.Create(LeadtoolsExamples.Common.ImagesPath.Path + "Image1_ToDib.bmp")
      Dim writer As New BinaryWriter(fs)
      writer.Write(bmfh.bfType)
      writer.Write(bmfh.bfSize)
      writer.Write(bmfh.bfReserved1)
      writer.Write(bmfh.bfReserved2)
      writer.Write(bmfh.bfOffBits)

      Dim buffer(1023) As Byte
      Dim offset As Integer = 0
      While (offset < dibSize)
         Dim bytes As Integer = Math.Min(dibSize - offset, buffer.Length)
         Marshal.Copy(New IntPtr(pdib.ToInt64() + offset), buffer, 0, bytes)
         fs.Write(buffer, 0, bytes)

         offset = offset + bytes
      End While
   End Using

   GlobalUnlock(hdib)
   GlobalFree(hdib)
End Sub
C#Copy Code
      [DllImport("Kernel32", CharSet = CharSet.Auto)] 
static extern IntPtr GlobalSize(IntPtr hMem); 
[DllImport("Kernel32", CharSet = CharSet.Auto)] 
static extern IntPtr GlobalLock(IntPtr hMem); 
 
[DllImport("Kernel32", CharSet = CharSet.Auto)] 
static extern int GlobalUnlock(IntPtr hMem); 
 
[DllImport("Kernel32", CharSet = CharSet.Auto)] 
static extern IntPtr GlobalFree(IntPtr hMem); 
 
[StructLayout(LayoutKind.Sequential, Pack = 1)] 
class BITMAPFILEHEADER 

   public ushort bfType; 
   public uint bfSize; 
   public ushort bfReserved1; 
   public ushort bfReserved2; 
   public uint bfOffBits; 

 
[StructLayout(LayoutKind.Sequential, Pack = 1)] 
class BITMAPINFOHEADER 

   public uint biSize; 
   public int biWidth; 
   public int biHeight; 
   public ushort biPlanes; 
   public ushort biBitCount; 
   public uint biCompression; 
   public uint biSizeImage; 
   public int biXPelsPerMeter; 
   public int biYPelsPerMeter; 
   public uint biClrUsed; 
   public uint biClrImportant; 

 
[StructLayout(LayoutKind.Sequential, Pack = 1)] 
class RGBQUAD 

   public byte rgbBlue; 
   public byte rgbGreen; 
   public byte rgbRed; 
   public byte rgbReserved; 

 
 
public void ChangeToDibExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Load an image 
   RasterImage image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp", 24, CodecsLoadByteOrder.BgrOrGray, 1, 1); 
 
   // Change to DIB 
   IntPtr hdib = image.ChangeToDib(RasterConvertToDibType.BitmapInfoHeader); 
 
   // Dispose the image since it is unusable now 
   image.Dispose(); 
 
   // Clean up 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
 
   // Save this DIB to disk as a BMP file 
 
   int dibSize = GlobalSize(hdib).ToInt32(); 
 
   BITMAPFILEHEADER bmfh = new BITMAPFILEHEADER(); 
   bmfh.bfType = 0x4d42; // "BM" 
   bmfh.bfSize = (uint)(Marshal.SizeOf(typeof(BITMAPFILEHEADER)) + dibSize); 
   bmfh.bfReserved1 = 0; 
   bmfh.bfReserved2 = 0; 
 
   IntPtr pdib = GlobalLock(hdib); 
 
   BITMAPINFOHEADER bmih = new BITMAPINFOHEADER(); 
   Marshal.PtrToStructure(pdib, bmih); 
 
   bmfh.bfOffBits = (uint)(Marshal.SizeOf(typeof(BITMAPFILEHEADER)) + bmih.biSize + bmih.biClrUsed * Marshal.SizeOf(typeof(RGBQUAD))); 
 
   using(FileStream fs = File.Create(LeadtoolsExamples.Common.ImagesPath.Path + "Image1_ToDib.bmp")) 
   { 
      BinaryWriter writer = new BinaryWriter(fs); 
      writer.Write(bmfh.bfType); 
      writer.Write(bmfh.bfSize); 
      writer.Write(bmfh.bfReserved1); 
      writer.Write(bmfh.bfReserved2); 
      writer.Write(bmfh.bfOffBits); 
 
      byte[] buffer = new byte[1024]; 
      int offset = 0; 
      while(offset < dibSize) 
      { 
         int bytes = Math.Min(dibSize - offset, buffer.Length); 
         Marshal.Copy(new IntPtr(pdib.ToInt64() + offset), buffer, 0, bytes); 
         fs.Write(buffer, 0, bytes); 
 
         offset += bytes; 
      } 
   } 
 
   GlobalUnlock(hdib); 
   GlobalFree(hdib); 
}

Remarks

A DIB consists of one of the following:

  • a BITMAPFILEHEADER,
  • a BITMAPV4HEADER (introduced in Windows 95 and Windows NT 4.0),
  • or a BITMAPV5HEADER (introduced in Windows 2000 and Windows 98),

followed by a color table and then the bitmap data. The resulting DIB type is determined by the value of the type parameter.

The orientation of the image and color order will depend on how the image was loaded into the RasterImage.

When you no longer need the DIB, you can free it using the Windows GlobalFree function.

This method does not support signed data images. It will throw an exception of type RasterException with code value RasterExceptionCode.SignedDataNotSupported if this RasterImage has a signed data image.

For more information on DDBs and DIBs, refer to Using DIBs, DDBs, and the Clipboard.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also