GdiPlus[50]: 图像(二) 关于 Create


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();


 
   

你可能感兴趣的:(create)