←Select platform

ChangeFromDib Method

Summary
Changes a Windows Device Independent Bitmap (DIB) to a LEAD RasterImage object.
Syntax
C#
C++/CLI
Python
public static RasterImage ChangeFromDib( 
   IntPtr hdib 
) 
public: 
static RasterImage^ ChangeFromDib(  
   IntPtr hdib 
)  
def ChangeFromDib(self,hdib): 

Parameters

hdib
The DIB handle to be changed.

Return Value

The newly created RasterImage object.

Remarks

This method results in only one copy of the image, and it invalidates the DIB handle.

This method supports the standard DIB formats (BI_RGB and BI_BITFIELDS) as well as some FOURCC (Four Character Code) formats that some capture cards output.

The following formats are the FOURCC formats that LEADTOOLS currently supports:

  • YVU9 (YUV9)
  • I420 (YUV12)
  • YUV2
  • YV12
  • IF09
  • IYUV
  • UYVY
  • cyuv
  • YUY2
  • YVYU
  • Y41P
  • Y211
  • Y41T
  • Y42T

This function does not support signed images.

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

Example
C#
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 image 
	RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "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(); 
 
	// 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(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:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.5.16
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.