如何使用C#在图片框中添加标注?

问题描述 投票:0回答:1

我需要使用C#在picturebox上添加WedgeRectCallout标注。

有办法吗?

请使用以下链接参考图片了解标注。

enter image description here

在word文档中,我可以使用Spire.Doc库并在代码下面编写相同的代码:

 ShapeObject Shape1 = para1.AppendShape(30, 50, ShapeType.WedgeRectCallout);
c# winforms
1个回答
0
投票

这是一个最小的例子。它创建了一个带有嵌套PanelTextBox子类。

请注意,您无法在设计器中向PictureBox添加控件。相反,你可以将它放在顶部并使用Nest函数将其嵌入PBox中。

(我使用一个技巧来简化边框的绘制:因为缩小GraphicsPath很难,我懒得写出其中两个,我画两次边框并添加一点偏移。效果是阴影效果,看起来很讨厌作弊..)

这是实际的课程:

enter image description here

这是类代码:

class WedgeCallout : Panel
{
  public WedgeCallout()
  {
    tb = new TextBox();
    Controls.Add(tb);
  }

  TextBox tb = null;
  GraphicsPath gp = new GraphicsPath();

  protected override void OnLayout(LayoutEventArgs levent)
  {
    tb.Size = new Size(Width - 10, (int)(Height * 0.66));
    tb.Location = new Point(5, 5);
    tb.BackColor = BackColor;
    tb.ForeColor = ForeColor ;
    tb.BorderStyle = BorderStyle.None;
    tb.Text = "Hiho";
    tb.Multiline = true;
    tb.TextAlign = HorizontalAlignment.Center;
    tb.Font = Font;
    SetRegion();
    base.OnLayout(levent);
  }

  protected override void OnBackColorChanged(EventArgs e)
  {
    base.OnBackColorChanged(e);
    if (BackColor != Color.Transparent) 
        tb.BackColor = BackColor;
  }

  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    if (Tag == null) return;

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    using(SolidBrush brush = new SolidBrush(tb.BackColor))
        e.Graphics.FillPath(brush, (GraphicsPath)Tag);
    using (Pen pen = new Pen(Color.DarkGray, 2f))
        e.Graphics.DrawPath(pen, (GraphicsPath)Tag);
    e.Graphics.TranslateTransform(-1, -1);
    using (Pen pen = new Pen(ForeColor, 2f))
        e.Graphics.DrawPath(pen, (GraphicsPath)Tag);
  }

  void SetRegion()
  {
    Rectangle r = ClientRectangle;
    int h = (int)(r.Height * 0.75f);
    if (gp != null) gp.Dispose();
    gp = new GraphicsPath();
    gp.AddPolygon(new PointF[]{   new Point(0,0),
        new Point(r.Width-1, 0),  new Point(r.Width-1, h),
        new PointF(50, h) ,       new Point(0, r.Height-1),
        new PointF(20, h),        new PointF(0, h)});

    Region = new Region(gp);
    Tag = gp;
  }
}

您可以在设计器中设置颜色和字体。

Nest功能:

void Nest(Control child, Control parent)
{
    Point p0 = parent.PointToScreen(Point.Empty);
    Point p1 = child.PointToScreen(Point.Empty);
    child.Location = new Point(p1.X - p0.X, p1.Y - p0.Y);
    child.Parent = parent;
}

它是从控件所在的表格中调用的:Nest(wedgeCallout1, pictureBox1);

请注意,要在一个图像中使用pbox保存到标注,您需要

  • 将标注嵌入PBox中
  • 使用pbox.DrawToBitmap
  • 暂时将背景颜色设置为透明

例:

private void saveBtn_Click(object sender, EventArgs e)
{
    Size sz = pictureBox1.ClientSize;
    using (Bitmap bmp = new Bitmap(sz.Width, sz.Height))
    {
        Color old = wedgeCallout1.BackColor;
        wedgeCallout1.BackColor = Color.Transparent;
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        bmp.Save(filename, ImageFormat.Png);
        wedgeCallout1.BackColor = old;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.