May 13, 2005

[Delphi] GDI+ VCL TBitmap へのイメージロード

gdiplus05

GDI+bitmap クラス(TGPBitmap)と graphics クラスを使って、画像ファイルから VCLTBitmap へイメージをロードする関数をつくった。

これで、VCLTBitmap でも *.bmp *.jpg *.gif *.png *.tif *.exif の画像ファイルを読み込める。

コードは
uses
  GDIPAPI, GDIPOBJ;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.AutoSize := true;
end;

function GetPixelFormatGP(bmp: TGPBitmap):Graphics.TPixelFormat;
var
  pf:GDIPAPI.TPixelFormat;
begin
  pf := bmp.GetPixelFormat;
  case GetPixelFormatSize(pf) of
    1: result := pf1bit;
    4: result := pf4bit;
    8: result := pf8bit;
    16: result := pf16bit;
    24: result := pf24bit;
    32: result := pf32bit;
  else
    result := pf32bit;
  end;
end;

function ImageLoaderGP(filename: string; dstbmp: TBitmap):Boolean;
var
  graphics : TGPGraphics;
  srcbmp: TGPBitmap;
  width, height: integer;
begin
  result := false;
  srcbmp := TGPBitmap.Create(filename);
  try
    width  := srcbmp.GetWidth;
    height := srcbmp.GetHeight;

    dstbmp.Width := width;
    dstbmp.Height := height;
    dstbmp.PixelFormat := GetPixelFormatGP(srcbmp);

    graphics := TGPGraphics.Create(dstbmp.Canvas.Handle);
    try
      if graphics.DrawImage(srcbmp,0,0,width,height) <> Ok then exit;
      result := true;
    finally
      graphics.Free;
    end;
  finally
    srcbmp.Free;
  end;  
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp:TBitmap;
begin
  bmp := TBitmap.Create;
  try
    if not ImageLoaderGP('test.tif',bmp) then exit;
    Image1.Picture.Bitmap.Assign(bmp);
  finally
    bmp.Free;
  end;
end;

GDIPAPI.pasTPixelFormat

  // In-memory pixel data formats:
  // bits 0-7 = format index
  // bits 8-15 = pixel size (in bits)
  // bits 16-23 = flags
  // bits 24-31 = reserved

type
  PixelFormat = Integer;
  TPixelFormat = PixelFormat;

const
  PixelFormatIndexed     = $00010000; // Indexes into a palette
  PixelFormatGDI         = $00020000; // Is a GDI-supported format
  PixelFormatAlpha       = $00040000; // Has an alpha component
  PixelFormatPAlpha      = $00080000; // Pre-multiplied alpha
  PixelFormatExtended    = $00100000; // Extended color 16 bits/channel
  PixelFormatCanonical   = $00200000;

  PixelFormatUndefined      = 0;
  PixelFormatDontCare       = 0;

  PixelFormat1bppIndexed    = (1  or ( 1 shl 8) or PixelFormatIndexed or PixelFormatGDI);
  PixelFormat4bppIndexed    = (2  or ( 4 shl 8) or PixelFormatIndexed or PixelFormatGDI);
  PixelFormat8bppIndexed    = (3  or ( 8 shl 8) or PixelFormatIndexed or PixelFormatGDI);
  PixelFormat16bppGrayScale = (4  or (16 shl 8) or PixelFormatExtended);
  PixelFormat16bppRGB555    = (5  or (16 shl 8) or PixelFormatGDI);
  PixelFormat16bppRGB565    = (6  or (16 shl 8) or PixelFormatGDI);
  PixelFormat16bppARGB1555  = (7  or (16 shl 8) or PixelFormatAlpha or PixelFormatGDI);
  PixelFormat24bppRGB       = (8  or (24 shl 8) or PixelFormatGDI);
  PixelFormat32bppRGB       = (9  or (32 shl 8) or PixelFormatGDI);
  PixelFormat32bppARGB      = (10 or (32 shl 8) or PixelFormatAlpha or PixelFormatGDI or PixelFormatCanonical);
  PixelFormat32bppPARGB     = (11 or (32 shl 8) or PixelFormatAlpha or PixelFormatPAlpha or PixelFormatGDI);
  PixelFormat48bppRGB       = (12 or (48 shl 8) or PixelFormatExtended);
  PixelFormat64bppARGB      = (13 or (64 shl 8) or PixelFormatAlpha  or PixelFormatCanonical or PixelFormatExtended);
  PixelFormat64bppPARGB     = (14 or (64 shl 8) or PixelFormatAlpha  or PixelFormatPAlpha or PixelFormatExtended);
  PixelFormatMax            = 15;

function GetPixelFormatSize(pixfmt: PixelFormat): UINT;
function IsIndexedPixelFormat(pixfmt: PixelFormat): BOOL;
function IsAlphaPixelFormat(pixfmt: PixelFormat): BOOL;
function IsExtendedPixelFormat(pixfmt: PixelFormat): BOOL;

この記事へのトラックバックURL