标记多边形

时间:2015-08-28 15:38:17

标签: c# winforms graphics polygon

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;
        }

1 个答案:

答案 0 :(得分:1)

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

        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