Creates a new
RasterImage from the specified Windows device dependent bitmap (DDB).
Syntax
Parameters
- hbitmap
- Handle to the Windows DDB.
- hpalette
- Handle to the Windows palette. This value can be IntPtr.Zero if the DDB is dependent on a device that is greater than 8 bits, or if the image will use the system palette.
Return Value
The newly create
RasterImage object.
Example
Visual Basic |
Copy Code |
<DllImport("gdi32")> _ Private Shared Function CreatePalette(ByVal lplgpl As LOGPALETTE256) As IntPtr End Function <DllImport("gdi32")> _ Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean End Function <StructLayout(LayoutKind.Sequential, Pack:=1)> _ Private Class LOGPALETTE256 Public palVersion As Short Public palNumEntries As Short <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1024)> _ Public palPalEntry As Byte() End Class
Public Sub FromHBitmapExample() RasterCodecs.Startup() Dim codecs As RasterCodecs = New RasterCodecs() Dim image As RasterImage = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1.CMP", 8, CodecsLoadByteOrder.Rgb, 1, 1)
Dim hbitmap As IntPtr = IntPtr.Zero hbitmap = image.ToHBitmap()
Dim tempForm As Form = New Form()
Dim colors As RasterColor() = image.GetPaintColors(tempForm.CreateGraphics())
Dim hPalette As IntPtr = IntPtr.Zero If Not colors Is Nothing AndAlso colors.Length <= 256 Then Dim log As LOGPALETTE256 = New LOGPALETTE256() log.palVersion = &H300 log.palNumEntries = CShort(colors.Length) log.palPalEntry = New Byte(1023) {}
Dim index As Integer = 0 Dim i As Integer = 0 Do While i < colors.Length log.palPalEntry(index) = colors(i).R index = index + 1 log.palPalEntry(index) = colors(i).G index = index + 1 log.palPalEntry(index) = colors(i).B index = index + 1 log.palPalEntry(index) = 0 index = index + 1 i += 1 Loop
hPalette = CreatePalette(log) End If
Dim destinationImage As RasterImage = RasterImage.FromHBitmap(hbitmap, hPalette)
If Not hPalette.Equals(IntPtr.Zero) Then DeleteObject(hPalette) End If
codecs.Save(destinationImage, LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1_FROMHBITMAP.BMP", RasterImageFormat.Bmp, 0)
destinationImage.Dispose() image.Dispose() codecs.Dispose() DeleteObject(hbitmap) RasterCodecs.Shutdown() End Sub |
C# |
Copy Code |
[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.Startup(); RasterCodecs codecs = new RasterCodecs(); // load image as 8-bit RasterImage image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1.CMP", 8, CodecsLoadByteOrder.Rgb, 1, 1); IntPtr hbitmap = IntPtr.Zero; hbitmap = image.ToHBitmap(); Form tempForm = new Form(); RasterColor[] colors = image.GetPaintColors(tempForm.CreateGraphics()); 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); } RasterImage destinationImage = RasterImage.FromHBitmap(hbitmap, hPalette); if(hPalette != IntPtr.Zero) DeleteObject(hPalette); codecs.Save(destinationImage, LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1_FROMHBITMAP.BMP", RasterImageFormat.Bmp, 0); destinationImage.Dispose(); image.Dispose(); codecs.Dispose(); DeleteObject(hbitmap); RasterCodecs.Shutdown(); } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also