LEADTOOLS GDI/GDI+(Leadtools.Drawingアセンブリ)

フレームを表示

FromHBitmap(IntPtr、IntPtr)メソッド






Windows DDBのハンドル。
Windowsパレットのハンドル。この値は、DDBが8ビットより大きいデバイスに依存しているならば、または、imageがシステムパレットを使うならば、IntPtr.Zeroでありえます。
指定されたWindowsデバイス依存ビットマップ(DDB)から、新しいLeadtools.RasterImageを作成します。
構文
public static RasterImage FromHBitmap( 
   IntPtr hbitmap,
   IntPtr hpalette
)
'Declaration
 
Public Overloads Shared Function FromHBitmap( _
   ByVal hbitmap As IntPtr, _
   ByVal hpalette As IntPtr _
) As RasterImage
'Usage
 
Dim hbitmap As IntPtr
Dim hpalette As IntPtr
Dim value As RasterImage
 
value = RasterImageConverter.FromHBitmap(hbitmap, hpalette)
public:
static RasterImage^ FromHBitmap( 
   IntPtr hbitmap,
   IntPtr hpalette
) 

パラメーター

hbitmap
Windows DDBのハンドル。
hpalette
Windowsパレットのハンドル。この値は、DDBが8ビットより大きいデバイスに依存しているならば、または、imageがシステムパレットを使うならば、IntPtr.Zeroでありえます。

戻り値

新しく、Leadtools.RasterImageオブジェクトを作成します。
解説

DDBとDIBの詳細については、「DIB、DDBおよびクリップボードの使用」を参照してください。

詳細については、「RasterImageとGDI/GDI+」を参照してください。

サンプル
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Drawing
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color

<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

<TestMethod()> _
Public Sub FromHBitmapExample()
   Dim codecs As New RasterCodecs()

   ' load image as 8-bit
   Using image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 8, CodecsLoadByteOrder.Rgb, 1, 1)
      Dim hbitmap As IntPtr = IntPtr.Zero
      hbitmap = RasterImageConverter.ToHBitmap(image)

      Dim colors() As RasterColor

      Using btmp As New Bitmap(1, 1)
         Using g As Graphics = Graphics.FromImage(btmp)
            colors = RasterImagePainter.GetPaintColors(image, g)
         End Using
      End Using

      Dim hPalette As IntPtr = IntPtr.Zero
      If Not IsNothing(colors) AndAlso colors.Length <= 256 Then
         Dim log As New LOGPALETTE256()
         log.palVersion = &H300
         log.palNumEntries = CType(colors.Length, Short)
         ReDim log.palPalEntry(1023)

         Dim index As Integer = 0
         For i As Integer = 0 To colors.Length - 1
            log.palPalEntry(index + 0) = colors(i).R
            log.palPalEntry(index + 1) = colors(i).G
            log.palPalEntry(index + 2) = colors(i).B
            log.palPalEntry(index + 3) = 0
            index = index + 4
         Next

         hPalette = CreatePalette(log)
      End If

      Using destinationImage As RasterImage = RasterImageConverter.FromHBitmap(hbitmap, hPalette)
         codecs.Save(destinationImage, Path.Combine(LEAD_VARS.ImagesDir, "Image1_FromHBitmap.bmp"), RasterImageFormat.Bmp, 0)
      End Using

      If hPalette <> IntPtr.Zero Then
         DeleteObject(hPalette)
      End If

      DeleteObject(hbitmap)
   End Using

   codecs.Dispose()
End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
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;
}

[TestMethod]
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:\Users\Public\Documents\LEADTOOLS Images";
}
必要条件

ターゲットプラットホーム

参照

参照

RasterImageConverterクラス
RasterImageConverterメンバ
オーバーロードリスト
ToHBitmap(RasterImage)メソッド
ChangeFromHBitmap(IntPtr、IntPtr)メソッド
ChangeToHBitmap(RasterImage)メソッド
RasterImage.FromDib
RasterImage.ToDib
RasterImage.ChangeFromDib
RasterImage.ChangeToDib