相關屬性、方法、函數:
IGPImage.Width; { 寬度(單位是像素) }
IGPImage.Height; { 高度(單位是像素) }
IGPImage.HorizontalResolution; { 獲取水平分辨率(以"像素/英寸"為單位) }
IGPImage.VerticalResolution; { 獲取垂直分辨率(以"像素/英寸"為單位) }
IGPImage.RawFormat; { 獲取圖像的文件格式 }
IGPImage.PixelFormat; { 獲取圖像的像素格式 }
IGPImage.Flags; { 獲取圖像像素的屬性標志 }
IGPImage.ImageType; { 圖像類型(Bitmap/Metafile/Unknown) }
IGPImage.GetFrameCount(); { 獲取幀數 }
function GetPixelFormatSize(); { 返回指定像素格式的顏色深度(每個像素的位數) }
function IsIndexedPixelFormat(); { 判斷像素格式是否是索引的 }
function IsAlphaPixelFormat(); { 判斷像素格式是否包含 alpha 信息 }
function IsCanonicalPixelFormat(); { 像素格式是否是每個像素 32 位(標准) }
function IsExtendedPixelFormat(); { 像素格式是否是每個像素 64 位 }
尺寸與分辨率:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Image: IGPImage;
Size: TGPSizeF;
Rect: TGPRectF;
SrcUnit: TGPUnit;
dx,dy: Single;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Apple.gif');
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Image, 10, 10);
ShowMessageFmt('Width:%d, Height:%d', [Image.Width, Image.Height]);
{ Width:120, Height:128 }
Image.GetPhysicalDimension(Size);
ShowMessageFmt('Width:%f, Height:%f', [Size.Width, Size.Height]);
{ Width:120.00, Height:128.00 }
Image.GetBounds(Rect, SrcUnit);
ShowMessageFmt('Width:%f, Height: %f; 單位: %d', [Size.Width, Size.Height, Ord(SrcUnit)]);
{ Width:120.00, Height:128.00; 單位: 2 } //TGPUnit(2) = UnitPixel;
dx := Image.HorizontalResolution;
dy := Image.VerticalResolution;
ShowMessageFmt('水平分辨率: %f, 垂直分辨率: %f', [dx, dy]);
{ 水平分辨率: 96.00, 垂直分辨率: 96.00 }
end;
文件格式:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: IGPImage;
f: string;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
if IsEqualGUID(Image.RawFormat, ImageFormatUndefined) then f := 'Undefined';
if IsEqualGUID(Image.RawFormat, ImageFormatMemoryBMP) then f := 'MemoryBMP';
if IsEqualGUID(Image.RawFormat, ImageFormatBMP) then f := 'BMP';
if IsEqualGUID(Image.RawFormat, ImageFormatEMF) then f := 'EMF';
if IsEqualGUID(Image.RawFormat, ImageFormatWMF) then f := 'WMF';
if IsEqualGUID(Image.RawFormat, ImageFormatJPEG) then f := 'JPEG';
if IsEqualGUID(Image.RawFormat, ImageFormatPNG) then f := 'PNG';
if IsEqualGUID(Image.RawFormat, ImageFormatGIF) then f := 'GIF';
if IsEqualGUID(Image.RawFormat, ImageFormatTIFF) then f := 'TIFF';
if IsEqualGUID(Image.RawFormat, ImageFormatEXIF) then f := 'EXIF';
if IsEqualGUID(Image.RawFormat, ImageFormatIcon) then f := 'Icon';
ShowMessage(f); { BMP }
end;
像素格式:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: IGPImage;
pf: string;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
case Image.PixelFormat of
PixelFormat1bppIndexed : pf := 'PixelFormat1bppIndexed';
PixelFormat4bppIndexed : pf := 'PixelFormat4bppIndexed';
PixelFormat8bppIndexed : pf := 'PixelFormat8bppIndexed';
PixelFormat16bppGrayScale : pf := 'PixelFormat16bppGrayScale';
PixelFormat16bppRGB555 : pf := 'PixelFormat16bppRGB555';
PixelFormat16bppRGB565 : pf := 'PixelFormat16bppRGB565';
PixelFormat16bppARGB1555 : pf := 'PixelFormat16bppARGB1555 ';
PixelFormat24bppRGB : pf := 'PixelFormat24bppRGB';
PixelFormat32bppRGB : pf := 'PixelFormat32bppRGB';
PixelFormat32bppARGB : pf := 'PixelFormat32bppARGB';
PixelFormat32bppPARGB : pf := 'PixelFormat32bppPARGB';
PixelFormat48bppRGB : pf := 'PixelFormat48bppRGB';
PixelFormat64bppARGB : pf := 'PixelFormat64bppARGB';
PixelFormat64bppPARGB : pf := 'PixelFormat64bppPARGB';
end;
ShowMessage(pf); { PixelFormat8bppIndexed }
end;
像素屬性:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
const
n = sLineBreak;
var
Image: IGPImage;
f: TGPImageFlags;
s: string;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Bird.bmp');
f := Image.Flags;
if TGPImageFlag(0) in f then s := s + 'ImageFlagsScalable' + n;
if TGPImageFlag(1) in f then s := s + 'ImageFlagsHasAlpha' + n;
if TGPImageFlag(2) in f then s := s + 'ImageFlagsHasTranslucent' + n;
if TGPImageFlag(3) in f then s := s + 'ImageFlagsPartiallyScalable' + n;
if TGPImageFlag(4) in f then s := s + 'ImageFlagsColorSpaceRGB' + n;
if TGPImageFlag(5) in f then s := s + 'ImageFlagsColorSpaceCMYK' + n;
if TGPImageFlag(6) in f then s := s + 'ImageFlagsColorSpaceGRAY' + n;
if TGPImageFlag(7) in f then s := s + 'ImageFlagsColorSpaceYCBCR' + n;
if TGPImageFlag(8) in f then s := s + 'ImageFlagsColorSpaceYCCK' + n;
if TGPImageFlag(12) in f then s := s + 'ImageFlagsHasRealDPI' + n;
if TGPImageFlag(13) in f then s := s + 'ImageFlagsHasRealPixelSize' + n;
if TGPImageFlag(16) in f then s := s + 'ImageFlagsReadOnly' + n;
if TGPImageFlag(17) in f then s := s + 'ImageFlagsCaching' + n;
ShowMessage(s);
{ ImageFlagsColorSpaceRGB / ImageFlagsHasRealPixelSize / ImageFlagsReadOnly }
end;
圖像類型:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: IGPImage;
str: string;
begin
ChDir('C:\GdiPlusImg\');
Image := TGPImage.Create('SampleMetafile.emf');
case Image.ImageType of
ImageTypeUnknown : str := 'Unknown';
ImageTypeBitmap : str := 'Bitmap';
ImageTypeMetafile: str := 'Metafile';
end;
ShowMessage(str); { Metafile }
end;
幾個圖像相關的全局函數:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: IGPImage;
n: Integer;
b: Boolean;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg');
{ 每個像素的大小 }
n := GetPixelFormatSize(Image.PixelFormat);
ShowMessage(IntToStr(n)); { 24 }
{ 是否是索引色 }
b := IsIndexedPixelFormat(Image.PixelFormat);
ShowMessage(BoolToStr(b, True)); { False }
{ 是否包含透明信息 }
b := IsAlphaPixelFormat(Image.PixelFormat);
ShowMessage(BoolToStr(b, True)); { False }
{ 是否是 32 位 }
b := IsCanonicalPixelFormat(Image.PixelFormat);
ShowMessage(BoolToStr(b, True)); { False }
{ 是否是 64 位 }
b := IsExtendedPixelFormat(Image.PixelFormat);
ShowMessage(BoolToStr(b, True)); { False }
end;
是否是多幀圖像(只有 TIFF 和 GIF 才可能是多幀):
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Tiff,Gif: IGPImage;
n: Integer;
begin
Tiff := TGPImage.Create('C:\GdiPlusImg\MultiFrame.tif');
Gif := TGPImage.Create('C:\GdiPlusImg\Apple.gif');
if IsEqualGUID(Tiff.RawFormat, ImageFormatTIFF) then
begin
n := Tiff.GetFrameCount(FrameDimensionPage);
ShowMessage(IntToStr(n)); //4
end;
if IsEqualGUID(Gif.RawFormat, ImageFormatGIF) then
begin
n := Gif.GetFrameCount(FrameDimensionTime);
ShowMessage(IntToStr(n)); //1
end;
end;