gpt4 book ai didi

delphi - 在椭圆内画一条线

转载 作者:行者123 更新时间:2023-12-01 20:26:49 25 4
gpt4 key购买 nike

我正在尝试在位图上绘制一个椭圆,其内部有一条线,类似于此图片:

image

我的应用程序可以加载位图图片,可以是任何尺寸。我所需要的只是画一个椭圆,里面有一条线。

我知道如何绘制椭圆,但我的问题是它内部的线条:

Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);
Bmp.Canvas.MoveTo(?, ?);// Here is my problem
Bmp.Canvas.LineTo(?, ?);// here too

我试试这个:

Bmp.Canvas.MoveTo(0, 0);
Bmp.Canvas.LineTo(Bmp.Width, Bmp.Height);

但这会从图片的左上角到右下角画一条线。

最佳答案

像您一样使用 Canvas.MoveTo()Canvas.LineTo() 就可以正常工作。您只需要限制椭圆内部线条的绘制,这样您在椭圆外部绘制的任何内容都不会被看到。

您可以应用椭圆 clipping region在绘制线条之前使用 Win32 API CreateEllipticRgn()CanvasSelectClipRgn()函数,例如:

// draw the actual ellipse first...
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);

// then create a region to match the ellipse...
Rgn := CreateEllipticRgn(0, 0, Bmp.Width, Bmp.Height);
try
SelectClipRgn(Bmp.Canvas.Handle, Rgn);
try
// then draw the line inside the region...
Bmp.Canvas.MoveTo(0, 0);
Bmp.Canvas.LineTo(Bmp.Width, Bmp.Height);
finally
SelectClipRgn(Bmp.Canvas.Handle, 0);
end;
finally
DeleteObject(Rgn);
end;

或者,您可以应用椭圆 clipping path相反,使用 Win32 API BeginPath() , EndPath() ,和 SelectClipPath()函数,例如:

// draw the actual ellipse first...
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);

// then create a path to match the ellipse...
BeginPath(Bmp.Canvas.Handle);
try
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);
finally
EndPath(Bmp.Canvas.Handle);
end;
SelectClipPath(Bmp.Canvas.Handle, RGN_COPY);

// then draw the line inside the path...
Bmp.Canvas.MoveTo(0, 0);
Bmp.Canvas.LineTo(Bmp.Width, Bmp.Height);

参见Clipping Overview在 MSDN 上了解更多详细信息。

例如:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
Bmp: TBitmap;
Rgn: HRGN;
begin
Bmp := TBitmap.Create;
try
Bmp.SetSize(Image1.Width, Image1.Height);

Bmp.Canvas.Brush.Color := clWhite;
Bmp.Canvas.FillRect(Rect(0, 0, Bmp.Width, Bmp.Height));

Bmp.Canvas.Pen.Color := clRed;
Bmp.Canvas.Pen.Width := 5;

// draw the actual ellipse first...
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);

// then create a region to match the ellipse...
Rgn := CreateEllipticRgn(0, 0, Bmp.Width, Bmp.Height);
try
SelectClipRgn(Bmp.Canvas.Handle, Rgn);
try
// then draw the line inside the region...
Bmp.Canvas.MoveTo(0, 0);
Bmp.Canvas.LineTo(Bmp.Width, Bmp.Height);
finally
SelectClipRgn(Bmp.Canvas.Handle, 0);
end;
finally
DeleteObject(Rgn);
end;

Image1.Picture.Assign(Bmp);
finally
Bmp.Free;
end;

Bmp := TBitmap.Create;
try
Bmp.SetSize(Image2.Width, Image2.Height);

Bmp.Canvas.Brush.Color := clWhite;
Bmp.Canvas.FillRect(Rect(0, 0, Bmp.Width, Bmp.Height));

Bmp.Canvas.Pen.Color := clRed;
Bmp.Canvas.Pen.Width := 5;

// draw the actual ellipse first...
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);

// then create a path to match the ellipse...
BeginPath(Bmp.Canvas.Handle);
try
Bmp.Canvas.Ellipse(0, 0, Bmp.Width, Bmp.Height);
finally
EndPath(Bmp.Canvas.Handle);
end;
SelectClipPath(Bmp.Canvas.Handle, RGN_COPY);

// then draw the line inside the path...
Bmp.Canvas.MoveTo(0, 0);
Bmp.Canvas.LineTo(Bmp.Width, Bmp.Height);

Image2.Picture.Assign(Bmp);
finally
Bmp.Free;
end;
end;

end.

image

关于delphi - 在椭圆内画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49866408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com