[英]Delphi drawing a smooth pie
Does anyone have a code for drawing a smooth pie on a canvas in Delphi? 有人在Delphi中有用于在画布上绘制平滑派的代码吗?
All I could find on the web are examples of smooth lines but that does not help me much... Also I considered Gr32, but for what I need, it adds a pretty big footprint to my app. 我在网上只能找到平滑线条的示例,但这对我没有多大帮助。我也考虑过Gr32,但是对于我所需要的,它为我的应用程序增加了很大的空间。 I just need to draw a filled pie on a canvas that does not look so awful as it does with the default 我只需要在画布上画一个填充的饼,看起来不像默认情况下那样糟糕
Canvas.Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4);
I use Delphi XE, so no Firemonkey... 我使用Delphi XE,所以没有Firemonkey ...
The pie on the left is obtained following your recommendation @johan using the following code: 根据您的建议@johan,使用以下代码获取左侧的饼图:
procedure TForm1.Button4Click(Sender: TObject);
var
Center:TPoint;
Radius:integer;
Bmp: TBitmap;
MF: TMetaFile;
MetafileCanvas: TMetafileCanvas;
begin
MF := TMetaFile.Create;
MF.Width := 150;
MF.Height := 150;
MetafileCanvas := TMetafileCanvas.Create(MF, 0);
MetafileCanvas.Pen.Color := clRed;
MetafileCanvas.Brush.Color := clRed;
MetafileCanvas.Brush.Style := bsSolid;
MetafileCanvas.Pie(0,0,150, 150, 150,50, 150,50);
MetafileCanvas.Free;
Bmp := Gdip.DrawAntiAliased(MF);
Image2.Picture.Assign(Bmp);
Bmp.Free;
MF.Free;
end;
Thanks 谢谢
This could be done easily with GDI+. 使用GDI +可以很容易地做到这一点。 The Key is using SmoothingModeAntiAlias
: 关键是使用SmoothingModeAntiAlias
:
uses GDIPAPI, GDIPOBJ;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
p: TGPPen;
b: TGPBrush;
r: TGPRect;
begin
r := MakeRect(10, 10, ClientWidth - 20, ClientHeight - 20);
g := TGPGraphics.Create(Canvas.Handle);
g.SetSmoothingMode(SmoothingModeAntiAlias);
b := TGPSolidBrush.Create(aclGreen);
p := TGPPen.Create(aclRed, 1);
g.FillPie(b, r, 45, 90);
g.DrawPie(p, r, 45, 90);
b.Free;
p.Free;
g.Free;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Invalidate;
end;
Tested with Delphi5 经过Delphi5测试
For drawing anti-aliased you can use: synopse gdiplus library, see: http://synopse.info/fossil/wiki?name=GDI%2B 要进行抗锯齿绘制,可以使用:synopse gdiplus库,请参阅: http ://synopse.info/fossil/wiki?name=GDI%2B
Here's an example: 这是一个例子:
implementation
uses
SynGDIPlus; //provides gdip (see below)
procedure TForm1.FormResize(Sender: TObject);
var Bmp: TBitmap;
MF: TMetaFile;
MetafileCanvas: TMetafileCanvas;
begin
MF := TMetaFile.Create;
MF.Width := 300;
MF.Height := 200;
MetafileCanvas := TMetafileCanvas.Create(MF, 0);
MetafileCanvas.Brush.Color := clRed;
MetafileCanvas.Brush.Style := bsDiagCross;
MetafileCanvas.Pie(0,0,50, 50, 300,50, 200,50);
MetafileCanvas.Free;
Bmp := Gdip.DrawAntiAliased(MF);
Image1.Picture.Assign(Bmp);
Bmp.Free;
//Image1.Picture.Assign(MF);
MF.Free;
end;
Using this unit you only pull in that single unit, nothing more. 使用此单元,您只需拉入单个单元,仅此而已。
You don't have to use a metafile, you can also draw directly to a DC, internally SynGDIPlus does however use a metafile. 您不必使用图元文件,也可以直接绘制到DC,但是SynGDIPlus在内部使用图元文件。
Faster alternative 更快的选择
If performance is an issue the Graphics32 library will be much faster. 如果性能是一个问题,则Graphics32库将更快。
http://graphics32.org/wiki/ http://graphics32.org/wiki/
I think that you worry far too much about the footprint of graphics32. 我认为您过于担心graphics32的占用空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.