←Select platform

ToHBitmap(RasterImage,IntPtr) Method

Summary
Creates a Windows Device-Dependent Bitmap (DDB) from a Leadtools.RasterImage.
Syntax
C#
C++/CLI
Python
public static IntPtr ToHBitmap( 
   RasterImage image, 
   IntPtr hdc 
) 
public: 
static IntPtr ToHBitmap(  
   RasterImage^ image, 
   IntPtr hdc 
)  
def ToHBitmap(self,image,hdc): 

Parameters

image
The source image

hdc
Handle to the Windows device responsible for the conversion. The mapping mode of the device context must be MM_TEXT.

Return Value

A handle to the Windows DDB this method creates.

Remarks

Converts a Leadtools.RasterImage object into a Windows device dependent bitmap (DDB). When this method is completed, there are two copies of the image in memory: the DDB and the original Leadtools.RasterImage. Freeing one will not affect the other.

NOTE: This method returns the data in an unmanaged handle. The caller is responsible for freeing the DDB's handle when it is no longer needed using the DeleteObject Windows API, otherwise, your application will leak resources.

For more information on DDBs, DIBs, GDI, and GDI+ refer to Using DIBs, DDBs, and the Clipboard and RasterImage and GDI/GDI+.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
 
[DllImport("gdi32")] 
private static extern IntPtr CreatePalette(LOGPALETTE256 lplgpl); 
[DllImport("gdi32")] 
private static extern bool DeleteObject(IntPtr hObject); 
 
[StructLayout(LayoutKind.Sequential, Pack = 1)] 
private class LOGPALETTE256 
{ 
   public short palVersion; 
   public short palNumEntries; 
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)] 
   public byte[] palPalEntry; 
} 
 
public void FromHBitmapExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // load image as 8-bit 
   using (RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 8, CodecsLoadByteOrder.Rgb, 1, 1)) 
   { 
      IntPtr hbitmap = IntPtr.Zero; 
      hbitmap = RasterImageConverter.ToHBitmap(image); 
 
      RasterColor[] colors; 
 
      using (Bitmap btmp = new Bitmap(1, 1)) 
      { 
         using (Graphics g = Graphics.FromImage(btmp)) 
         { 
            colors = RasterImagePainter.GetPaintColors(image, g); 
         } 
      } 
 
      IntPtr hPalette = IntPtr.Zero; 
      if (colors != null && colors.Length <= 256) 
      { 
         LOGPALETTE256 log = new LOGPALETTE256(); 
         log.palVersion = 0x300; 
         log.palNumEntries = (short)colors.Length; 
         log.palPalEntry = new byte[1024]; 
 
         int index = 0; 
         for (int i = 0; i < colors.Length; i++) 
         { 
            log.palPalEntry[index++] = colors[i].R; 
            log.palPalEntry[index++] = colors[i].G; 
            log.palPalEntry[index++] = colors[i].B; 
            log.palPalEntry[index++] = 0; 
         } 
 
         hPalette = CreatePalette(log); 
      } 
 
      using (RasterImage destinationImage = RasterImageConverter.FromHBitmap(hbitmap, hPalette)) 
      { 
         codecs.Save(destinationImage, Path.Combine(LEAD_VARS.ImagesDir, "Image1_FromHBitmap.bmp"), RasterImageFormat.Bmp, 0); 
      } 
 
      if (hPalette != IntPtr.Zero) 
      { 
         DeleteObject(hPalette); 
      } 
 
      DeleteObject(hbitmap); 
   } 
 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

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

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