LEADTOOLS(Leadtoolsアセンブリ)

フレームを表示

ChangeToDibメソッド








作成するDIBの型。設定可能な値は、以下の通りです。
意味
RasterConvertToDibType.BitmapInfoHeader BITMAPINFOHEADERを使うDIB
RasterConvertToDibType.BitmapV4Header BITMAPV4HEADERを使うDIB。(Windows 95とWindows NT 4.0で持ち出される)
RasterConvertToDibType.BitmapV5Header BITMAPV5HEADER(Windows 2000とWindows 98で持ち出される)を使うDIB
Windowsデバイス非依存ビットマップ(DIB)に、LEAD RasterImageオブジェクトを変更します。
構文
public IntPtr ChangeToDib( 
   RasterConvertToDibType type
)
'Declaration
 
Public Function ChangeToDib( _
   ByVal type As RasterConvertToDibType _
) As IntPtr
'Usage
 
Dim instance As RasterImage
Dim type As RasterConvertToDibType
Dim value As IntPtr
 
value = instance.ChangeToDib(type)
public IntPtr ChangeToDib( 
   RasterConvertToDibType type
)
 function Leadtools.RasterImage.ChangeToDib( 
   type 
)
public:
IntPtr ChangeToDib( 
   RasterConvertToDibType type
) 

パラメーター

作成するDIBの型。設定可能な値は、以下の通りです。
意味
RasterConvertToDibType.BitmapInfoHeader BITMAPINFOHEADERを使うDIB
RasterConvertToDibType.BitmapV4Header BITMAPV4HEADERを使うDIB。(Windows 95とWindows NT 4.0で持ち出される)
RasterConvertToDibType.BitmapV5Header BITMAPV5HEADER(Windows 2000とWindows 98で持ち出される)を使うDIB

戻り値

このメソッドが作成するデバイス非依存ビットマップ(DIB)へのハンドル。
解説

DIBは、以下の1つから構成されます:

カラーテーブル、それからビットマップデータが続きます。結果として生じるDIB型は、引数の値によって決定されます。

imageとカラーオーダーの向きは、imageがどのようにRasterImageにロードされたかに依存します。

DIBをもはや必要としないとき、Windows GlobalFree関数を用いてそれを開放することができます。

このメソッドは、符合付きデータイメージをサポートしません。このRasterImageに符合付きデータイメージがあるならば、それはコード値RasterExceptionCode.SignedDataNotSupportedで型RasterExceptionの例外をスローします。

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

サンプル

このサンプルはRasterImageをロードして、V5 DIBにそれを変更して、ビットマップファイルとしてDIBを書きます。

Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Controls
Imports Leadtools.Dicom
Imports Leadtools.Drawing
Imports Leadtools.Svg

<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalSize(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalLock(ByVal hMem As IntPtr) As IntPtr
End Function

<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalUnlock(ByVal hMem As IntPtr) As Integer
End Function

<DllImport("Kernel32", CharSet:=CharSet.Auto)> _
Shared Function GlobalFree(ByVal hMem As IntPtr) As IntPtr
End Function

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class BITMAPFILEHEADER
   Public bfType As UShort
   Public bfSize As UInteger
   Public bfReserved1 As UShort
   Public bfReserved2 As UShort
   Public bfOffBits As UInteger
End Class

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class BITMAPINFOHEADER
   Public biSize As UInteger
   Public biWidth As Integer
   Public biHeight As Integer
   Public biPlanes As UShort
   Public biBitCount As UShort
   Public biCompression As UInteger
   Public biSizeImage As UInteger
   Public biXPelsPerMeter As Integer
   Public biYPelsPerMeter As Integer
   Public biClrUsed As UInteger
   Public biClrImportant As UInteger
End Class

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class RGBQUAD
   Public rgbBlue As Byte
   Public rgbGreen As Byte
   Public rgbRed As Byte
   Public rgbReserved As Byte
End Class

      
Public Sub ChangeToDibExample()
   Dim codecs As New RasterCodecs()

   ' Load an image
   Dim image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1)

   ' Change to DIB
   Dim hdib As IntPtr = 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

   Dim 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 = 0
   bmfh.bfReserved2 = 0

   Dim 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 Byte
      Dim offset As Integer = 0
      While (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 + bytes
      End While
   End Using

   GlobalUnlock(hdib)
   GlobalFree(hdib)
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.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(ImagesPath.Path, "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(ImagesPath.Path, "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);
}
必要条件

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

参照

参照

RasterImageクラス
RasterImageメンバ
FromDib(IntPtr)メソッド
FromDibArrayメソッド
ToDibArrayメソッド
RasterImageConverter.FromHBitmap
ToDibメソッド
RasterImageConverter.ToHBitmap
ChangeFromDibメソッド
RasterImageConverter.ChangeFromHBitmap
RasterImageConverter.ChangeToHBitmap