GdiPlus[37]: IGPGraphicsPath (四) 路径变换


IGPGraphicsPath.Flatten();   //把路径中的曲线转换为近似直线段(路径中只有 Bezier 线和直线).
IGPGraphicsPath.Outline();   //同 Flatten();
IGPGraphicsPath.Warp();      //四边形或平行四边形扭曲. 
IGPGraphicsPath.Widen();     //把轮廓转换为范围.
IGPGraphicsPath.Transform(); //矩阵变换; 其实上面几个方法都包含矩阵变换的参数.

 
 
 
 
 

 

 

  

Flatten 测试图:

26153115_5HlB.png

Flatten 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPBrush;
  Path, PathClone: IGPGraphicsPath;
  Rect: TGPRect;
  Pt: TGPPoint;
  Matrix: IGPMatrix;
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Pen := TGPPen.Create($FFFF0000);
  Brush := TGPSolidBrush.Create($800000FF);
  Path := TGPGraphicsPath.Create;
  Rect.Initialize(20, 20, 200, 75);
  Path.AddEllipse(Rect);

  PathClone := Path.Clone;
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Flatten(); //其参数 2 默认是 0.25
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Flatten(nil, 10);
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Flatten(nil, 25);
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Flatten(nil, 50);
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);

  PathClone := Path.Clone;
  Matrix := TGPMatrix.Create;
  Matrix.Scale(1, 0.5);
  Matrix.Translate(0, 20);
  PathClone.Flatten(Matrix, 25);
  Graphics.DrawPath(Pen, PathClone);
  for Pt in PathClone.PathPointsI do Graphics.FillRectangle(Brush, Pt.X-2, Pt.Y-2, 4, 4);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);
end;

 
 
 
 
 

 

 

  

Warp 测试图:

26153115_wF1Z.png

Warp 测试代码:

uses GdiPlus;

const
  { 数组有三个元素时, 0: 左上; 1: 右上; 2: 左下; 3: 自动调整为平行四边形 }
  pts3: array[0..2] of TGPPointF = ((X:20; Y:20),(X:250; Y:0),(X:20; Y:120));

  { 数组有四个元素时, 0: 左上; 1: 右上; 2: 左下; 3: 右下 }
  pts4: array[0..3] of TGPPointF = ((X:20; Y:20),(X:220; Y:10),(X:20; Y:120),(X:270; Y:190));

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Brush: IGPHatchBrush;
  Path,PathClone: IGPGraphicsPath;
  Rect: TGPRectF;
  FontFamily: IGPFontFamily;
  StringFormat: IGPStringFormat;
  Pt: TGPPointF;
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Brush := TGPHatchBrush.Create(HatchStyleShingle, $FF808080, $FF483D8B);

  Rect.Initialize(20, 20, 250, 120);
  FontFamily := TGPFontFamily.Create('Arial Black');
  StringFormat := TGPStringFormat.Create;
  StringFormat.Alignment := StringAlignmentCenter;
  StringFormat.LineAlignment := StringAlignmentCenter;

  Path := TGPGraphicsPath.Create;
  Path.AddRectangle(Rect);
  Path.AddString('Delphi', FontFamily, [], 56, Rect, StringFormat);

  PathClone := Path.Clone;
  Graphics.FillPath(Brush, PathClone);
  Graphics.TranslateTransform(0, Rect.Y*2 + Rect.Height);

  //PathClone := Path.Clone;
  PathClone.Warp(pts3, Rect);
  Graphics.FillPath(Brush, PathClone);
  Graphics.TranslateTransform(0, Rect.Y*2 + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Warp(pts4, Rect);
  Graphics.FillPath(Brush, PathClone);
  Graphics.TranslateTransform(0, Rect.Y*2 + Rect.Height);
end;

 
 
 
 
 

 

 

  

Widen 测试图:

26153116_i1RZ.png

Widen 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
  Brush: IGPHatchBrush;
  Path, PathClone: IGPGraphicsPath;
  Rect: TGPRect;
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Pen := TGPPen.Create($AAFF0000, 20);
  Brush := TGPHatchBrush.Create(HatchStyleShingle, $FFC0C0C0, $FF888888);
  Path := TGPGraphicsPath.Create;
  Rect.Initialize(20, 20, 200, 75);
  Path.AddEllipse(Rect);

  PathClone := Path.Clone;
  Graphics.FillPath(Brush, PathClone);
  Graphics.DrawPath(Pen, PathClone);
  Graphics.TranslateTransform(0, Rect.Y*2 + Rect.Height);

  PathClone := Path.Clone;
  PathClone.Widen(Pen);
  Graphics.FillPath(Brush, PathClone);
  Pen.Width := 1;
  Graphics.DrawPath(Pen, PathClone);
  Graphics.TranslateTransform(0, Rect.Y + Rect.Height);
end;

 
 
 
 
 

 

 

  

Translate 测试图:

26153116_334O.png

Translate 测试代码:

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Brush: IGPHatchBrush;
  Path,PathClone: IGPGraphicsPath;
  FontFamily: IGPFontFamily;
  Pt: TGPPointF;
  Matrix: IGPMatrix; 
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Brush := TGPHatchBrush.Create(HatchStyleZigZag, $FFC0C0C0, $FFFF0000);

  FontFamily := TGPFontFamily.Create('Arial Black');
  Pt.Initialize(0, 0);
  Path := TGPGraphicsPath.Create;
  Path.AddString('Delphi', FontFamily, [], 32, Pt, nil);

  Graphics.FillPath(Brush, Path);

  PathClone := Path.Clone;
  Matrix := TGPMatrix.Create;
  Matrix.Scale(2, 2);
  Matrix.Rotate(-30);
  Matrix.Translate(-35, 45);
  PathClone.Transform(Matrix);
  Graphics.FillPath(Brush, PathClone);
  
  PathClone := Path.Clone;
  Matrix.Reset;
  Matrix.Scale(2.5, 6);
  Matrix.Translate(0, 18);
  PathClone.Transform(Matrix);
  Graphics.FillPath(Brush, PathClone);
end;

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/319376

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值