相关属性、方法、函数:
IGPImage.Width; { 宽度(单位是像素) }
IGPImage.Height; { 高度(单位是像素) }
IGPImage.HorizontalResolution; { 获取水平分辨率(以"像素/英寸"为单位) }
IGPImage.VerticalResolution; { 获取垂直分辨率(以"像素/英寸"为单位) }
IGPImage.RawFormat; { 获取图像的文件格式 }
IGPImage.PixelFormat; { 获取图像的像素格式 }
IGPImage.Flags; { 获取图像像素的属性标志 }
IGPImage.ImageType; { 图像类型(Bitmap/Metafile/Unknown) }
IGPImage.Palette; { 获取或设置调色板 }
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;
调色板:
uses GdiPlus;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: IGPImage;
Palette: IGPColorPalette;
i: Integer;
str: string;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Stripes.bmp');
Palette := Image.Palette;
ShowMessage(IntToStr(Image.Palette.Count)); //5; 若是 0 表示无调色板
{ 也可用下面这句判断是否使用了索引色(或者说调色板) }
//if not IsIndexedPixelFormat(Image.PixelFormat) then Exit;
{ 以字符串的形式获取调试板中的所有颜色 }
for i := 0 to Palette.Count - 1 do
begin
str := str + Format('$%.8X', [Palette.Entries[i]]) + sLineBreak;
end;
ShowMessage(str); { $FF000000/$FFFFFFFF/$FF0000FF/$FF00FF00/$FFFF0000 }
end;