用 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.

看看效果:

欢迎微信搜一搜 IT软件部落 关注公众号,你可以了解更详细的内容

欢儿微信扫码关注 IT软件部落 公众号,你可以了解更详细的内容

delphi Image32 路径的更多相关文章

  1. delphi android路径 TPath 文件路径,文件管理

    获取Android相关文档路径 delphi 新路径.文件功能 IOUtils单元,文件路径,文件管理 http://docwiki.embarcadero.com/RADStudio/Berlin/ ...

  2. Delphi - 本地路径的创建、清空本地指定文件夹下的文件

    本地路径的创建 在做下载操作时,我们一般先把文件下载到本地指定的路径下,然后再做其他使用. 为了防止程序出现异常,我们通常需要先判断本地是否存在指定的路径. 以C盘Tmp文件夹为例,我们可以这样做,代 ...

  3. delphi 相对路径

    ..代表上级目录 .代表当前目录 \代表目录分隔 ..\..\表上上一级目录

  4. Delphi 文件操作(路径、目录)

    Delphi利用系统环境变量获取常用系统目录 //譬如 %WINDIR% 是表示系统目录的系统变量, 可以这样获取: var s: string; begin s := GetEnvironmentV ...

  5. 使用 InstallShield 制作 Delphi 软件安装包

    软件版本: InstallShield 12 Delphi 5/7 SQL Server 2005 一.配置软件信息 二.软件安装的需求配置 三.安装架构 四.安装需要的文件 软件的安装路径.可执行文 ...

  6. 在Delphi中编写res文件

    delphiimagedosinterfaceborland脚本先用记事本编写一个rc的文件. 如内容为: _Comms RCData Comms.jpg Comms.jpg为图片名称, 然后在这个r ...

  7. Delphi Bpl包学习

    对于BPL包,我个人理解是:就是一种封装方式,和DLL,EXE类似,把代码放到包(package)里面保存而已. 一.先说说如何创建BPL包 1.   打开delphi IDE(delphi7 为例) ...

  8. 在Delphi中编辑res文件

    先用记事本编写一个rc的文件.如内容为:_Comms RCData Comms.jpg Comms.jpg为图片名称,然后在这个rc文件和图片拷贝到delphi安装路径的bin文件夹里面,选中这两个文 ...

  9. Delphi BusinessSkinForm使用说明

    1.先放bsBusinessSkinForm.bsSkinData.bsStoredSkin各一个到窗体上 2.修改bsBusinessSkinForm的SkinData属性为bsSkinData1 ...

  10. Object Pascal对象模型中构造函数之研究

    http://www.delphi2007.net/delphiblog/html/delphi_2004511950333715.html 前言 近期,一直在使用 C++ 与 Object Pasc ...

随机推荐

  1. Python 潮流周刊#66:Python 的预处理器(摘要)

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  2. MATLAB 绘制 K 线图

    需要安装 Financial Toolbox. % 示例数据 openPrices = [100, 102, 104, 103, 105]; highPrices = [105, 107, 106, ...

  3. Standard Quorum Intersection

    标准定足数交集 定义和背景 系统模型: 系统中有 \(n\) 个节点,其中最多 \(f\) 个节点可能是拜占庭故障节点(恶意节点). 为了保证容忍 \(f\) 个拜占庭节点,系统通常需要至少 \(3f ...

  4. 使用命令行 Windows 修改文件权限

    修改文件访问权限的命令行工具是 icacls,其使用语法是这样的: icacls <file> # 查看文件的访问权限 icacls <file> /grant <SID ...

  5. chroot 整理

    chroot 是什么? 就是change root directory ,比如默认是 /, 可以用这个chroot 把 / 换成其他指定的目录 chroot 干什么的? 增加了系统的安全性,限制了用户 ...

  6. python之re库,正则表达

    一.前言 为什么要学re库呢?这里主要学他的正则表达,在编写安全脚本的时候肯定要遇到一些不规则的匹配规则,当然编写爬虫也少不了正则匹配去找到一些具有特殊特征的字符串.因此这是十分必要的,然而.re库使 ...

  7. TypeScript 高级教程 – TypeScript 类型体操 (第三篇)

    前言 在 第一部 – 把 TypeScript 当强类型语言使用 和 第二部 – 把 TypeScript 当编程语言使用 后, 我们几乎已经把 TypeScript 的招数学完了. 第三部就要开始做 ...

  8. Spring —— AOP总结

    AOP 总结                    

  9. Spring —— 核心概念

    IoC (Inversion of Control) 控制反转 使用对象时,由主动new产生对象转换为由外部提供对象,对象的创建控制权由程序转移至外部,这种思想成为控制反转 Spring技术对IoC思 ...

  10. QT6窗口系统之QT底层窗口QWindow:QT框架中哪些常见窗口是基于QWindow的? 如何实现QT框架栅格窗口?如何实现QT框架OpenGL窗口?

    QT6窗口系统之QT底层窗口QWindow:QT框架中哪些常见窗口是基于QWindow的? 如何实现QT框架栅格窗口?如何实现QT框架OpenGL窗口? 简介 本文介绍了QT6窗口系统中的QT底层窗口 ...