gpt4 book ai didi

c# - 标记多边形

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:05 24 4
gpt4 key购买 nike

enter image description here我有一个用于创建点数组的函数,因此我可以绘制一个多边形,我想在这个多边形内部添加标签,我知道可以使用拉绳或标签控件来完成,但我想确保我有在我的例子中,多边形的方向相同,它们是矩形,还根据形状大小控制字体大小。

我试图在 Draw 字符串中创建一个 rect,但没有任何想法

 private void BuildImage()
{
Graphics refGraph = this.CreateGraphics();
IntPtr hdc = refGraph.GetHdc();
Metafile image = new Metafile(hdc, EmfType.EmfOnly, "Shapes");
using (var g = Graphics.FromImage(image))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
SolidBrush myBrush = new SolidBrush(Color.Black);
Pen p = new Pen(Color.Red, 0.2f);
foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor)
{
RectangleF rec = new RectangleF((float)(resistorRow.CenterX - resistorRow.Length / 2), (float)(resistorRow.CenterY - resistorRow.Width / 2), (float)resistorRow.Length, (float)resistorRow.Width);
float orientation = 360 - (float)resistorRow.Orientation;
PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY);
PointF[] points = CreatePolygon(rec, center, orientation);
if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY))
{
g.FillEllipse(myBrush, (float)resistorRow.HiX - 0.5f, (float)resistorRow.HiY - 0.5f, 1, 1);
g.DrawLine(p, new PointF((float)resistorRow.HiX, (float)resistorRow.HiY), center);
}

g.FillPolygon(myBrush, points);

Font drawFont = new Font("cursor", 1);
SolidBrush textBrush = new SolidBrush(Color.Blue);
//g.Transform = matrix;
g.DrawString(resistorRow.ComponentName, drawFont, textBrush, points[0]);
}
}
refGraph.ReleaseHdc(hdc);
refGraph.Dispose();
Image = image;

}


private PointF[] CreatePolygon(RectangleF rec, PointF center, float orientation)
{
PointF TL = new PointF(rec.Left, rec.Top);
PointF TR = new PointF(rec.Right, rec.Top);
PointF BL = new PointF(rec.Left, rec.Bottom);
PointF BR = new PointF(rec.Right, rec.Bottom);
PointF[] points = new PointF[] { BL, TL, TR, BR, BL };
matrix = new System.Drawing.Drawing2D.Matrix();
matrix.RotateAt(orientation, center);
matrix.TransformPoints(points);
return points;
}

最佳答案

将旋转矩阵应用于您正在使用的图形上下文:

        e.Graphics.Transform = matrix;
e.Graphics.FillPolygon(textBrush, points);
e.Graphics.DrawString(resistorRow.ComponentName, drawFont, textBrush2, TL);

enter image description here

编辑:一些示例代码:

public class ResistorWithLabel
{
public string ComponentName { get; set; }
public RectangleF Rect { get; set; }
public float Orientation { get; set; }
public Color BackgroundColor { get; set; }
public Color ForegroundColor { get; set; }
public int FontSize { get; set; }

public void Draw(Graphics g)
{
Matrix contextMatrix = g.Transform;
Matrix matrix = new Matrix();
matrix.RotateAt(Orientation, new PointF((Rect.Left+Rect.Right)/2, (Rect.Top+Rect.Bottom)/2));

SolidBrush polygonBrush = new SolidBrush(BackgroundColor);
SolidBrush textBrush = new SolidBrush(ForegroundColor);
Font font = new Font("Courier", FontSize);

PointF TL = new PointF(Rect.Left, Rect.Top);
PointF TR = new PointF(Rect.Right, Rect.Top);
PointF BL = new PointF(Rect.Left, Rect.Bottom);
PointF BR = new PointF(Rect.Right, Rect.Bottom);
PointF[] points = new PointF[] { BL, TL, TR, BR };

g.Transform = matrix;
g.FillPolygon(polygonBrush, points);
g.DrawString(ComponentName, font, textBrush, TL);
g.Transform = contextMatrix;
}
}

private void Form3_Paint(object sender, PaintEventArgs e)
{
ResistorWithLabel r1 = new ResistorWithLabel();
r1.ComponentName = "Resistor 1";
r1.Rect = new RectangleF(50, 100, 100, 50);
r1.Orientation = 25;
r1.BackgroundColor = Color.Blue;
r1.ForegroundColor = Color.Yellow;
r1.FontSize = 16;
r1.Draw(e.Graphics);

ResistorWithLabel r2 = new ResistorWithLabel();
r2.ComponentName = "Resistor 2";
r2.Rect = new RectangleF(200, 100, 200, 100);
r2.Orientation = 75;
r2.BackgroundColor = Color.Gray;
r2.ForegroundColor = Color.Orange;
r2.FontSize = 32;
r2.Draw(e.Graphics);
}

enter image description here

关于c# - 标记多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32274813/

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