IGPImage 類提供的 Create 方法:
Image := TGPImage.Create(文件(或流), Boolean);
//其中的布爾值默認 False; 如果為 True 則使用文件(或流)中的已嵌入顏色管理信息來進行顏色校正.
{ 示例1: 從文件建立 }
var
Graphics: IGPGraphics;
Image: IGPImage;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Grapes.jpg' );
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Image, 10, 10);
end;
{ 示例2: 從流(IStream)建立 }
const
path = 'C:\GdiPlusImg\Grapes.jpg';
var
Graphics: IGPGraphics;
Image: IGPImage;
Stream: IStream; { IStream 聲明中 ActiveX 單元 }
begin
Stream := TStreamAdapter.Create(TFileStream.Create(path, fmOpenRead), soOwned);
Image := TGPImage.Create(Stream);
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Image, 10, 10);
end;
IGPBitmap 類提供的 Create 方法:
// 1. 作為 IGPImage 繼承者, IGPBitmap 也可以使用 IGPImage 的建立方法; 另外:
// 2. 可以指定寬度、高度和像素格式, 像素格式默認 PixelFormat32bppARGB:
var
Bitmap: IGPBitmap;
begin
Bitmap := TGPBitmap.Create(100, 100);
ShowMessage(IntToStr(Bitmap.PixelFormat)); { 2498570 }
Bitmap := TGPBitmap.Create(100, 100, PixelFormat32bppARGB);
ShowMessage(IntToStr(Bitmap.PixelFormat)); { 2498570 }
end;
// 3. 可以指定寬度、高度和 Graphics(使用其分辨率):
var
Graphics: IGPGraphics;
Bitmap: IGPBitmap;
bx,by,gx,gy: Single;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
Bitmap := TGPBitmap.Create(100, 100, Graphics);
bx := Bitmap.HorizontalResolution;
by := Bitmap.VerticalResolution;
gx := Graphics.DpiX;
gy := Graphics.DpiY;
ShowMessageFmt('%g:%g; %g:%g', [bx, by, gx, gy]); {96:96; 96:96}
end;
// 4. 從另一個圖像采集像素建立:
var
Graphics: IGPGraphics;
BitmapTmp, Bitmap: IGPBitmap;
Brush: IGPSolidBrush;
Rect: TGPRect;
BitmapData: TGPBitmapData;
begin
BitmapTmp := TGPBitmap.Create(16, 16);
Graphics := TGPGraphics.Create(BitmapTmp);
Brush := TGPSolidBrush.Create(0);
Brush.Color := $FFFF0000;
Graphics.FillRectangle(Brush, 0, 0, 8, 16);
Brush.Color := $FF0000FF;
Graphics.FillRectangle(Brush, 8, 0, 8, 16);
Rect.Initialize(0, 0, BitmapTmp.Width, BitmapTmp.Height);
BitmapData := BitmapTmp.LockBits(Rect, [ImageLockModeRead], BitmapTmp.PixelFormat);
Bitmap := TGPBitmap.Create(100, 100, 4, PixelFormat32bppARGB, BitmapData.Scan0);
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Bitmap, 10, 10);
BitmapTmp := TGPBitmap.Create(16, 16);
end;
// 5. 從 HBITMAP 建立:
var
Graphics: IGPGraphics;
Bitmap: IGPBitmap;
bit: TBitmap;
begin
bit := TBitmap.Create;
bit.LoadFromFile('C:\GdiPlusImg\Bird.bmp');
Bitmap := TGPBitmap.Create(bit.Handle, bit.Palette);
bit.Free;
Graphics := TGPGraphics.Create(Handle);
Graphics.Clear($FFFFFFFF);
Graphics.DrawImage(Bitmap, 10, 10);
end;
// 6. 從 HICON 建立:
var
Graphics: IGPGraphics;
Bitmap: IGPBitmap;
begin
Bitmap := TGPBitmap.Create(Application.Icon.Handle);
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Bitmap, 10, 10);
end;
// 7. 從資源文件中的圖片(好像只能是 bmp)建立(假定已添加一張命名為 Bitmap_1 的資源圖片):
var
Graphics: IGPGraphics;
Bitmap: IGPBitmap;
begin
Bitmap := TGPBitmap.Create(HInstance, 'Bitmap_1');
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Bitmap, 10, 10);
end;
// 8. 還可以通過 TBitmapInfo 建立.
// 9. DirectDrawSurface7 建立; 這是不是讓 GDI+ 和 DirectX 結合的起點呢? 找時間再學習.
IGPMetafile 類提供的 Create 方法:
// 1. 作為 IGPImage 繼承者, IGPMetafile 也可以使用 IGPImage 的建立方法:
var
Graphics: IGPGraphics;
Metafile: IGPMetafile;
begin
Metafile := TGPMetafile.Create('C:\GdiPlusImg\SampleMetafile.emf');
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Metafile, 10, 10);
end;
// 2. 從文件建立並繪圖(文件不存在則建立, 存在則覆蓋; 此時必須同時指定 HDC):
var
GraphicsMeta, Graphics: IGPGraphics;
Metafile: IGPMetafile;
Pen: IGPPen;
begin
Metafile := TGPMetafile.Create('C:\GdiPlusImg\Test.emf', Canvas.Handle);
GraphicsMeta := TGPGraphics.Create(Metafile);
Pen := TGPPen.Create($80FF0000);
GraphicsMeta.DrawRectangle(Pen, 0, 0, 50, 30);
GraphicsMeta := nil; { 這樣才結束繪圖 }
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Metafile, 10, 10);
end;
// 3. 可在建立是指定文件尺寸; 此時最好同時指定尺寸的單位, 否則默認單位是: MetafileFrameUnitGdi
var
GraphicsMeta, Graphics: IGPGraphics;
Metafile: IGPMetafile;
Pen: IGPPen;
Rect: TGPRect;
begin
Rect.Initialize(0, 0, 100, 100);
Metafile := TGPMetafile.Create(Canvas.Handle, Rect, MetafileFrameUnitPixel);
GraphicsMeta := TGPGraphics.Create(Metafile);
Pen := TGPPen.Create($80FF0000);
GraphicsMeta.DrawRectangle(Pen, 0, 0, 50, 30);
GraphicsMeta := nil;
Text := Format('%d, %d', [Metafile.Width, Metafile.Height]);
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Metafile, 10, 10);
end;
// 4. 可指定圖元文件類型:
var
GraphicsMeta, Graphics: IGPGraphics;
Metafile: IGPMetafile;
Pen: IGPPen;
begin
Metafile := TGPMetafile.Create(Canvas.Handle, EmfTypeEmfPlusOnly);
GraphicsMeta := TGPGraphics.Create(Metafile);
Pen := TGPPen.Create($80FF0000);
GraphicsMeta.DrawRectangle(Pen, 0, 0, 50, 30);
GraphicsMeta := nil;
Text := Format('%d, %d', [Metafile.Width, Metafile.Height]);
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Metafile, 10, 10);
end;
{ 圖元文件有三種類型: }
// EmfOnly 文件中所有記錄都是 EMF 記錄, 可通過 GDI 或 GDI+ 顯示.
// EmfPlusOnly 文件中所有記錄都是 EMF+ 記錄, 可通過 GDI+ 顯示, 而不能通過 GDI 顯示.
// EmfPlusDual 文件中所有記錄都是雙份的(EMF+ 與 EMF), 可通過 GDI 或 GDI+ 顯示.
// 5. 可在建立是寫入描述文本:
var
GraphicsMeta, Graphics: IGPGraphics;
Metafile: IGPMetafile;
Pen: IGPPen;
begin
Metafile := TGPMetafile.Create(Canvas.Handle, EmfTypeEmfPlusOnly, 'Description_123');
GraphicsMeta := TGPGraphics.Create(Metafile);
Pen := TGPPen.Create($80FF0000);
GraphicsMeta.DrawRectangle(Pen, 0, 0, 50, 30);
GraphicsMeta := nil;
Graphics := TGPGraphics.Create(Handle);
Graphics.DrawImage(Metafile, 10, 10);
end;
// 圖元文件的建立參數還有很多花樣, 基本就是這些東西的組合.
還有一些 From... 函數和 Create 是一樣的:
Image := TGPImage.FromFile();
Image := TGPImage.FromStream();
Bitmap := TGPBitmap.FromFile();
Bitmap := TGPBitmap.FromStream();
Bitmap := TGPBitmap.FromDirectDrawSurface7();
Bitmap := TGPBitmap.FromBitmapInfo();
Bitmap := TGPBitmap.FromHBitmap();
Bitmap := TGPBitmap.FromHIcon();
Bitmap := TGPBitmap.FromResource();
Metafile := TGPMetafile.FromFile();
Metafile := TGPMetafile.FromStream();