public IntPtr ChangeToDib(RasterConvertToDibType type)
Public Function ChangeToDib( _ByVal type As RasterConvertToDibType _) As IntPtr
public:IntPtr ChangeToDib(RasterConvertToDibType type)
type
Type of DIB to create. Possible values are:
| Value | Meaning |
|---|---|
| RasterConvertToDibType.BitmapInfoHeader | DIB that uses a BITMAPINFOHEADER |
| RasterConvertToDibType.BitmapV4Header | DIB that uses a BITMAPV4HEADER. (Introduced in Windows 95 and Windows NT 4.0) |
| RasterConvertToDibType.BitmapV5Header | DIB that uses a BITMAPV5HEADER (Introduced in Windows 2000 and Windows 98) |
A handle to the device independent bitmap (DIB) this method creates.
A DIB consists of one of the following:
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.
This example loads a RasterImage, changes it to a V5 DIB and writes out the DIB as a bitmap file.
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Core;using Leadtools.ImageProcessing.Color;using Leadtools.Dicom;using Leadtools.Drawing;using Leadtools.Controls;using Leadtools.Svg;[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 codecs = new RasterCodecs();// Load an imageRasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1);// Change to DIBIntPtr hdib = image.ChangeToDib(RasterConvertToDibType.BitmapInfoHeader);// Dispose the image since it is unusable nowimage.Dispose();// Clean upcodecs.Dispose();// Save this DIB to disk as a BMP fileint 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(Path.Combine(LEAD_VARS.ImagesDir, "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);}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.CoreImports Leadtools.ImageProcessing.ColorImports Leadtools.ControlsImports Leadtools.DicomImports Leadtools.DrawingImports Leadtools.Svg<DllImport("Kernel32", CharSet:=CharSet.Auto)>Shared Function GlobalSize(ByVal hMem As IntPtr) As IntPtrEnd Function<DllImport("Kernel32", CharSet:=CharSet.Auto)>Shared Function GlobalLock(ByVal hMem As IntPtr) As IntPtrEnd Function<DllImport("Kernel32", CharSet:=CharSet.Auto)>Shared Function GlobalUnlock(ByVal hMem As IntPtr) As IntegerEnd Function<DllImport("Kernel32", CharSet:=CharSet.Auto)>Shared Function GlobalFree(ByVal hMem As IntPtr) As IntPtrEnd Function<StructLayout(LayoutKind.Sequential, Pack:=1)>Class BITMAPFILEHEADERPublic bfType As UShortPublic bfSize As UIntegerPublic bfReserved1 As UShortPublic bfReserved2 As UShortPublic bfOffBits As UIntegerEnd Class<StructLayout(LayoutKind.Sequential, Pack:=1)>Class BITMAPINFOHEADERPublic biSize As UIntegerPublic biWidth As IntegerPublic biHeight As IntegerPublic biPlanes As UShortPublic biBitCount As UShortPublic biCompression As UIntegerPublic biSizeImage As UIntegerPublic biXPelsPerMeter As IntegerPublic biYPelsPerMeter As IntegerPublic biClrUsed As UIntegerPublic biClrImportant As UIntegerEnd Class<StructLayout(LayoutKind.Sequential, Pack:=1)>Class RGBQUADPublic rgbBlue As BytePublic rgbGreen As BytePublic rgbRed As BytePublic rgbReserved As ByteEnd ClassPublic Sub ChangeToDibExample()Dim codecs As New RasterCodecs()' Load an imageDim image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1)' Change to DIBDim hdib As IntPtr = image.ChangeToDib(RasterConvertToDibType.BitmapInfoHeader)' Dispose the image since it is unusable nowimage.Dispose()' Clean upcodecs.Dispose()' Save this DIB to disk as a BMP fileDim 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 = 0bmfh.bfReserved2 = 0Dim 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(Path.Combine(LEAD_VARS.ImagesDir, "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 ByteDim offset As Integer = 0While (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 + bytesEnd WhileEnd UsingGlobalUnlock(hdib)GlobalFree(hdib)End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"End Class
RasterImageConverter.FromHBitmap
RasterImageConverter.ToHBitmap
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
