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

时间:2019-03-08 10:50:52

标签: c# winforms

我需要使用C#在图片框上添加WedgeRectCallout标注。

有办法吗?

请使用下面的链接引用图片以了解标注。

enter image description here

在Word文档中,我可以使用Spire.Doc库并编写以下代码来做同样的事情:

 ShapeObject Shape1 = para1.AppendShape(30, 50, ShapeType.WedgeRectCallout);

1 个答案:

答案 0 :(得分:0)

这是一个最小的示例。它使用嵌套的Panel创建一个TextBox子类。

请注意,您不能在设计器中将控件添加到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保存到一张图片中的标注,您需要

    Pli中的
  • 嵌套标注
  • 使用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;
    }
}