首页 > 其他分享 >delphi Image32 路径

delphi Image32 路径

时间:2024-06-13 21:12:02浏览次数:14  
标签:ImagePanel delphi 路径 srcRec Image32 DPIAware path Image dstRec

用 Image32的理由之一,也是路径这块做得比delphi(FMX)自带的要好,skia中支持svg,但对路径处理功能不够强大。VCL只能使用第三方库。

VCL如果要支持SVG,只有 Image32好点,SVGIconImageList 第三方库也使用 Image32.

 

 

unit uFrmPaths;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, //
  System.Types, System.Math, Img32, Img32.Panels, Img32.Vector, Img32.Extra,
  Img32.Fmt.PNG, Img32.Draw, Img32.Text, Vcl.ComCtrls;

type
  TfrmPaths = class(TForm)
    TabControl1: TTabControl;
    procedure FormCreate(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    arial12: TFontCache;
    arial16: TFontCache;
    ImagePanel: TImage32Panel;
    procedure ImagePanelClick(Sender: TObject);
    procedure DoClosedPaths1;
    procedure DoClosedPaths2;
    procedure DoOpenPaths;
  end;

var
  frmPaths: TfrmPaths;

implementation

{$R *.dfm}

procedure TfrmPaths.DoClosedPaths1;
var
  margin, adjustX: integer;
  path, smoothedPath: TPathD;
  srcRec, spRec, dstRec: TRect;
  Scale, dx, dy: double;
  str: UnicodeString;
begin
  margin := DPIAware(20);
  path := MakePath([190, 120, 240, 160, 560, 120, 190, 490]); // Img32.Vector  数组
  // get the bounds of the smoothpath with the largest bounds
  smoothedPath := SmoothPath(path, true, -1); // 路径点,是否闭合,张力    Img32.Extra
  spRec := GetBounds(smoothedPath);           // Img32.Vector

  // get dstRec
  dstRec := ImagePanel.InnerClientRect;               // 绑定区域
  System.Types.InflateRect(dstRec, -margin, -margin); // 缩小区域
  dstRec.Width := dstRec.Width div 3 - margin;        // 1/3 宽度,后面要显示3组图形
  inc(dstRec.Top, DPIAware(20));                      // making sure there's room for text
  dec(dstRec.Bottom, DPIAware(20));                   // making sure there's room for text
  adjustX := dstRec.Width + margin;
  // 绘制文本
  str := 'SmoothPath function - using different tensions(不同张力)'; // 这里的 中文 如果不能正常显示,是因为字体问题,请参考:FormCreate中描述
  DrawText(ImagePanel.Image, dstRec.Left, dstRec.Top - DPIAware(20), str, arial16);

  Scale := Min(dstRec.Width / spRec.Width, dstRec.Height / spRec.Height); // 最小比例
  path := ScalePath(path, Scale); // 根据比例缩放路径
  dx := dstRec.Left - spRec.Left * Scale;
  dy := dstRec.Top - spRec.Top * Scale;
  path := TranslatePath(path, dx, dy); // 平移路径
  srcRec := GetBounds(path);
  smoothedPath := SmoothPath(path, true, 0); // tensions :0
  // 第1组图形绘制
  DrawLine(ImagePanel.Image, path, DPIAware(1), clRed32, esClosed); // 绘制封闭的红线
  DrawLine(ImagePanel.Image, smoothedPath, DPIAware(2), clBlue32, esClosed); // 绘制封闭的蓝线
  DrawText(ImagePanel.Image, srcRec.Left, srcRec.Bottom + DPIAware(20), '0', arial16); // 绘制文字

  // ====
  path := TranslatePath(path, adjustX, 0);    // 平移路径
  TranslateRect(srcRec, adjustX, 0);          // 平均区域
  smoothedPath := SmoothPath(path, true, -1); // tensions :-1
  // 第2组图形绘制
  DrawLine(ImagePanel.Image, path, DPIAware(1), clRed32, esClosed); // 绘制封闭的红线
  DrawLine(ImagePanel.Image, smoothedPath, DPIAware(2), clBlue32, esClosed); // 绘制封闭的蓝线
  DrawText(ImagePanel.Image, srcRec.Left, srcRec.Bottom + DPIAware(20), '-1', arial16); // 绘制文字
  //
  path := TranslatePath(path, adjustX, 0);
  TranslateRect(srcRec, adjustX, 0);
  smoothedPath := SmoothPath(path, true, 0.5); // tensions :0.5
  // 第2组图形绘制
  DrawLine(ImagePanel.Image, path, DPIAware(1), clRed32, esClosed); // 绘制封闭的红线
  DrawLine(ImagePanel.Image, smoothedPath, DPIAware(2), clBlue32, esClosed); // 绘制封闭的蓝线
  DrawText(ImagePanel.Image, srcRec.Left, srcRec.Bottom + DPIAware(20), '0.5', arial16); // 绘制文字
end;

procedure TfrmPaths.DoClosedPaths2;
var
  i, j, maxX, maxY: integer;
  path, smoothedPath: TPathD;
  dstRec, srcRec: TRect;
  scaleX, scaleY: double;
const
  margin = 50;
  ptCount = 3;
begin
  SetLength(path, ptCount); // 3个点

  dstRec := ImagePanel.InnerClientRect;               // 客户区域
  System.Types.InflateRect(dstRec, -margin, -margin); // 缩小 margin
  maxX := dstRec.Width;
  maxY := dstRec.Height;
  for i := 0 to ptCount - 1 do
    path[i] := PointD(Random(maxX), Random(maxY)); // 随机产生3个点
  smoothedPath := SmoothPath(path, true, -0.5);    // 路径点,是否闭合,张力    Img32.Extra
  srcRec := GetBounds(smoothedPath);               // Img32.Vector
  scaleX := maxX / srcRec.Width;
  scaleY := maxY / srcRec.Height;
  path := ScalePath(path, scaleX, scaleY); // 根据比例缩放路径
  // repeat smoothing now that the path has been properly scaled
  smoothedPath := SmoothPath(path, true, -0.5);
  srcRec := GetBounds(smoothedPath);
  path := TranslatePath(path, margin - srcRec.Left, margin - srcRec.Top); // 路径平移
  smoothedPath := TranslatePath(smoothedPath, margin - srcRec.Left, margin - srcRec.Top);
  DrawLine(ImagePanel.Image, smoothedPath, DPIAware(2.5), clGreen32, esPolygon); // 绘制平滑的闭合路径线
  for j := 0 to High(path) do
  begin
    DrawPoint(ImagePanel.Image, path[j], DPIAware(3.5), clRed32); // 绘制点
    DrawText(ImagePanel.Image, path[j].X - 50, path[j].Y + 0, Format('[%f,%f]', [path[j].X, path[j].Y]), arial16); // 绘制文字
  end;
  DrawText(ImagePanel.Image, 30, 30, '点击后重新生成', arial16); // 绘制文字
end;

procedure TfrmPaths.DoOpenPaths;
var
  i, j, dx: integer;
  paths, smoothedPaths: TPathsD;
  rec: TRect;
const
  margin = 50;
  ptCount = 8;
  pathCount = 3;
begin
  rec := ImagePanel.InnerClientRect;               // 客户区域
  System.Types.InflateRect(rec, -margin, -margin); // 缩小 margin
  dx := rec.Width div (ptCount);                   // 点间距

  SetLength(paths, pathCount);         // 3个路径
  SetLength(smoothedPaths, pathCount); // 3个平滑路径

  for i := 0 to High(paths) do
  begin
    SetLength(paths[i], ptCount); // 每条路径 N 个点
    for j := 0 to High(paths[i]) do
      paths[i][j] := PointD(rec.Left + j * dx, rec.Bottom - Random(rec.Height)); // 每个点 随机值
  end;
  for i := 0 to High(smoothedPaths) do
    smoothedPaths[i] := SmoothPath(paths[i], false, 0); // 生成每条路径的平滑路径

  for i := 0 to High(smoothedPaths) do
  begin
    DrawLine(ImagePanel.Image, smoothedPaths[i], DPIAware(3), RainbowColor(i / pathCount), esSquare); // 绘制平滑路径 (非闭合)
    for j := 0 to High(paths[i]) do
      DrawPoint(ImagePanel.Image, paths[i][j], DPIAware(2.5), clRed32); // 绘制点
  end;
  DrawText(ImagePanel.Image, 30, 30, '点击后重新生成', arial16); // 绘制文字
end;

procedure TfrmPaths.FormCreate(Sender: TObject);
const
  // C_FontName='Arial';           //这个显示不了汉字
  // C_FontName='Arial Unicode MS';//名称 可以从 office Word字体下拉框查找(可以显示汉字)   [控制面板\所有控制面板项\字体]
  C_FontName = '方正舒体'; // 默认找不到字体(需要修改 TFontReader.Load 中 CreateFontIndirect )
var
  arialFont: TFontReader;
begin
  self.BorderStyle := bsNone;
  // Img32.Text中   TFontReader.Load 中 CreateFontIndirect 默认 logFont.lfCharSet:ANSI_CHARSET,很多中文字体是找不到的)
  // 因此,要将   logFont.lfCharSet= GB2312_CHARSET 即可找到中文字体 (如:方正舒体)
  FontManager.Load(C_FontName, 800);
  arialFont := FontManager.GetFont(C_FontName);
  arial12 := TFontCache.Create(arialFont, DPIAware(12));
  arial16 := TFontCache.Create(arialFont, DPIAware(16));
  ImagePanel := TImage32Panel.Create(self);
  ImagePanel.Parent := TabControl1;
  ImagePanel.Align := alClient;
  ImagePanel.OnClick := ImagePanelClick;
  ActiveControl := ImagePanel;
  ImagePanel.BorderWidth := 0; // 默认有14的边框.
  with ImagePanel.InnerClientRect do
    ImagePanel.Image.SetSize(Width, Height);
  TabControl1Change(nil);
end;

procedure TfrmPaths.FormDestroy(Sender: TObject);
begin
  arial12.Free;
  arial16.Free;
end;

procedure TfrmPaths.FormResize(Sender: TObject);
begin
  if Assigned(ImagePanel) then
  begin
    with ImagePanel.InnerClientRect do
      ImagePanel.Image.SetSize(Width, Height);
    TabControl1Change(nil);
  end;
end;

procedure TfrmPaths.ImagePanelClick(Sender: TObject);
begin
  if TabControl1.TabIndex <> 0 then
    TabControl1Change(nil);
end;

procedure TfrmPaths.TabControl1Change(Sender: TObject);
begin
  ImagePanel.Scale := 1.0;
  ImagePanel.Image.Clear;
  case TabControl1.TabIndex of
    0:
      DoClosedPaths1;
    1:
      DoClosedPaths2;
  else
    DoOpenPaths;
  end;
end;

end.

看看效果:

 

标签:ImagePanel,delphi,路径,srcRec,Image32,DPIAware,path,Image,dstRec
From: https://www.cnblogs.com/bluejade/p/18246759

相关文章

  • 多源最短路径算法 -- 弗洛伊德(Floyd)算法
    1. 简介        Floyd算法,全名为Floyd-Warshall算法,亦称弗洛伊德算法或佛洛依德算法,是一种用于寻找给定加权图中所有顶点对之间的最短路径的算法。这种算法以1978年图灵奖获得者、斯坦福大学计算机科学系教授罗伯特·弗洛伊德的名字命名。2.核心思想    ......
  • 【动态规划】| 路径问题之不同路径II 力扣63
    ......
  • 窥探 Delphi 的 dcp文件
    之前我猜测Delphi里的dcp类似java里的maven的pom.xml,经过测试发现,和猜想的才不多,既包含了pom.xml的信息,又包含了本bpl的信息;测试如下:在本文中,我将展示如何从Delphi的DCP文件中读取一些非常基本的信息。这包括要链接的BPL名称、所需的DCP和包含的单元。在过去的......
  • 虚拟机开机或重启后自动执行绝对路径下的可执行文件方法
    在虚拟机中打开路径/etc/systemd/system/在Linux系统中,使用service(或systemctl,对于使用systemd的系统)来在开机后自动执行某个可执行文件,通常涉及创建一个服务单元文件(serviceunitfile)。以下是一些常见的方法,用于设置开机自启服务来执行可执行文件:1.使用systemd(大多数现代......
  • 【CMake系列】06-项目结构与输出路径管理
    为了对大型项目实现更好的管理【模块化协作开发等等】,cmake提供了很多指令,可以对项目的结构进行调整、管理,便于项目的合理规划。本文我们要学习的就是项目结构的设置,以及构建程序等输出路径的设置本专栏的实践代码全部放在github上,欢迎star!!!如有问题,欢迎留言、或加群......
  • 路径总和-力扣
    本题想到的解法是对二叉树进行深度搜索,并记录路径和,当节点为叶子节点时,将路径和与目标值进行判断,如果相等则返回true,否则返回false,最后返回左右子树或的值即可,因为只需有一条满足条件就可以。/***Definitionforabinarytreenode.*structTreeNode{*intv......
  • 卫星通讯传输技术助力电力运维巡检效率提升:EasyCVR实现远程监控与管理的新路径
    随着科技的快速发展,视频监控技术已广泛应用于各个领域。而卫星通讯作为一种高效、稳定的通信方式,为视频监控系统的远程传输提供了有力支持。一、方案背景随着电力行业的快速发展,电力运维巡检工作变得愈发重要。传统的巡检方式往往受到地域、环境等因素的限制,难以实现对电力设备......
  • Image32 动画演示2
    Image32自带的Demo,添加一些注解。unituFrmAnimation2;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ExtCtrls,System.Math,Img3......
  • 前端使用 Konva 实现可视化设计器(14)- 折线 - 最优路径应用【代码篇】
    话接上回《前端使用Konva实现可视化设计器(13)-折线-最优路径应用【思路篇】》,这一章继续说说相关的代码如何构思的,如何一步步构建数据模型可供AStar算法进行路径规划,最终画出节点之间的连接折线。请大家动动小手,给我一个免费的Star吧~大家如果发现了Bug,欢迎来提Issue......
  • 二叉树的所有路径-力扣
    这道题目需要返回给定二叉树所有从根节点到叶子节点的路径,那么对二叉树进行深度优先搜索,遇到节点就将其加到路径中,如果这个节点的左右子节点都为空,那么它就是一个叶子节点,将这条路径加入到结果数组中。这里将int转换为string使用了to_string()函数。/***Definition......