前沿观查点 發表於 2024-6-13 20:58:00

delphi Image32 路径

<p>用 Image32的理由之一,也是路径这块做得比delphi(FMX)自带的要好,skia中支持svg,但对路径处理功能不够强大。VCL只能使用第三方库。</p>
<p>VCL如果要支持SVG,只有 Image32好点,SVGIconImageList&nbsp;第三方库也使用 Image32.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">unit</span><span style="color: rgba(0, 0, 0, 1)"> uFrmPaths;

</span><span style="color: rgba(0, 0, 255, 1)">interface</span>

<span style="color: rgba(0, 0, 255, 1)">uses</span><span style="color: rgba(0, 0, 0, 1)">
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, </span><span style="color: rgba(0, 128, 0, 1)">//
</span><span style="color: rgba(0, 0, 0, 1)">System.Types, System.Math, Img32, Img32.Panels, Img32.Vector, Img32.Extra,
Img32.Fmt.PNG, Img32.Draw, Img32.Text, Vcl.ComCtrls;

</span><span style="color: rgba(0, 0, 255, 1)">type</span><span style="color: rgba(0, 0, 0, 1)">
TfrmPaths </span>= <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">(TForm)
    TabControl1: TTabControl;
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> FormCreate(Sender: TObject);
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TabControl1Change(Sender: TObject);
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> FormDestroy(Sender: TObject);
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> FormResize(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)">
    arial12: TFontCache;
    arial16: TFontCache;
    ImagePanel: TImage32Panel;
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> ImagePanelClick(Sender: TObject);
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> DoClosedPaths1;
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> DoClosedPaths2;
    </span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> DoOpenPaths;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
frmPaths: TfrmPaths;

</span><span style="color: rgba(0, 0, 255, 1)">implementation</span>

<span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">$R *.dfm</span><span style="color: rgba(0, 128, 0, 1)">}</span>

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

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

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

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

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

</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TfrmPaths.DoOpenPaths;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
i, j, dx: integer;
paths, smoothedPaths: TPathsD;
rec: TRect;
</span><span style="color: rgba(0, 0, 255, 1)">const</span><span style="color: rgba(0, 0, 0, 1)">
margin </span>= <span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">;
ptCount </span>= <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">;
pathCount </span>= <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
rec :</span>= ImagePanel.InnerClientRect;               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 客户区域</span>
System.Types.InflateRect(rec, -margin, -margin); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缩小 margin</span>
dx := rec.Width <span style="color: rgba(0, 0, 255, 1)">div</span> (ptCount);                   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点间距</span>
<span style="color: rgba(0, 0, 0, 1)">
SetLength(paths, pathCount);         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3个路径</span>
SetLength(smoothedPaths, pathCount); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3个平滑路径</span>

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

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

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

</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TfrmPaths.FormDestroy(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
arial12.Free;
arial16.Free;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TfrmPaths.FormResize(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Assigned(ImagePanel) <span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 0, 255, 1)">begin</span>
    <span style="color: rgba(0, 0, 255, 1)">with</span> ImagePanel.InnerClientRect <span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)">
      ImagePanel.Image.SetSize(Width, Height);
    TabControl1Change(</span><span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TfrmPaths.ImagePanelClick(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> TabControl1.TabIndex &lt;&gt; <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    TabControl1Change(</span><span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TfrmPaths.TabControl1Change(Sender: TObject);
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
ImagePanel.Scale :</span>= <span style="color: rgba(128, 0, 128, 1)">1.0</span><span style="color: rgba(0, 0, 0, 1)">;
ImagePanel.Image.Clear;
</span><span style="color: rgba(0, 0, 255, 1)">case</span> TabControl1.TabIndex <span style="color: rgba(0, 0, 255, 1)">of</span>
    <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">:
      DoClosedPaths1;
    </span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">:
      DoClosedPaths2;
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    DoOpenPaths;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<p>看看效果:</p>
<p><img src="https://img2024.cnblogs.com/blog/1103754/202406/1103754-20240613205526687-1683081046.gif"></p>
<p>欢迎微信搜一搜&nbsp;<strong>IT软件部落</strong>&nbsp;关注公众号,你可以了解更详细的内容&nbsp;</p>
<p><img src="https://img2024.cnblogs.com/blog/1103754/202410/1103754-20241027211531941-862695125.jpg"></p>
<p>欢儿微信扫码关注&nbsp;<strong>IT软件部落</strong>&nbsp;公众号,你可以了解更详细的内容</p>
<p><img alt="" class="medium-zoom-image lazyload" data-src="https://img2024.cnblogs.com/blog/1103754/202410/1103754-20241027211540319-394205364.jpg"></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/bluejade/p/18246759
頁: [1]
查看完整版本: delphi Image32 路径