所以就像一个graphics.FillEllipse,但中间有一个洞。我需要通过在它们周围放一个环来突出显示一些圆形图标,并且由于较大程序的限制,很难/不可能简单地将FillEllipse放在它们下面以使其看起来像是一个洞。
答案 0 :(得分:15)
// Create a brush
SolidBrush b = new SolidBrush(Color.Blue);
// Clear your Graphics object (defined externally)
gfx.Clear(Color.White);
// You need a path for the outer and inner circles
GraphicsPath path1 = new GraphicsPath();
GraphicsPath path2 = new GraphicsPath();
// Define the paths (where X, Y, and D are chosen externally)
path1.AddEllipse((float)(X - D / 2), (float)(Y - D / 2), (float)D, (float)D);
path2.AddEllipse((float)(X - D / 4), (float)(Y - D / 4), (float)(D / 2), (float)(D / 2));
// Create a region from the Outer circle.
Region region = new Region(path1);
// Exclude the Inner circle from the region
region.Exclude(path2);
// Draw the region to your Graphics object
gfx.FillRegion(b, region);
答案 1 :(得分:14)
使用GDI +,您可以绘制一个笔宽较高的圆圈,使其看起来像甜甜圈。中心没有任何东西,所以你将能够看透它。
答案 2 :(得分:3)
您可以创建一个区域,该区域基于您使用FillEllipse绘制的内容,并使用Region的Exclude方法通过使用另一个对FillEllipse的调用返回的另一个GraphicsPath来删除您不想要的区域。 / p>
然后,您只需将生成的区域叠加在您希望它所包围的区域之上。
答案 3 :(得分:0)
基于user263134回答:
g.FillRegion(Brushes.Black, GetRingRegion(center, innerRadius, outherRadius));
public static RectangleF GetRectangle(PointF center, float radius)
{
var rectangle = new RectangleF(center.X - radius, center.Y - radius,radius * 2, radius * 2);
return rectangle;
}
public static Region GetRingRegion(PointF center, float innerRadius, float outherRadius)
{
// You need a path for the outer and inner circles
var path1 = new GraphicsPath();
var path2 = new GraphicsPath();
// Define the paths (where X, Y, and D are chosen externally)
path1.AddEllipse(GetRectangle(center,outherRadius));
path2.AddEllipse(GetRectangle(center, innerRadius));
// Create a region from the Outer circle.
Region region = new Region(path1);
// Exclude the Inner circle from the region
region.Exclude(path2);
return region;
}
答案 4 :(得分:0)
关于sth答案的最重要的事情是最灵活的答案,那就是需要处理GraphicsPath和Brush,因此将它们的声明放在using语句中,如下所示:
// Clear your Graphics object (defined externally)
gfx.Clear(Color.White);
// You need a path for the outer and inner circles
using (GraphicsPath path1 = new GraphicsPath(), path2 = new GraphicsPath())
{
// Define the paths (where X, Y, and D are chosen externally)
path1.AddEllipse((float)(X - D / 2), (float)(Y - D / 2), (float)D, (float)D);
path2.AddEllipse((float)(X - D / 4), (float)(Y - D / 4), (float)(D / 2), (float)(D / 2));
// Create a region from the Outer circle.
Region region = new Region(path1);
// Exclude the Inner circle from the region
region.Exclude(path2);
// Create a brush
using (SolidBrush b = new SolidBrush(Color.Blue))
{
// Draw the region to your Graphics object
gfx.FillRegion(b, region);
}
}
这将确保在不再需要它们时将其丢弃。
using是确保即使发生异常也可以调用Dispose方法的最佳方法。