简体   繁体   中英

How to add callout in picturebox using C#?

I need to add WedgeRectCallout callout on picturebox using C#.

Is there a way to do it?

please refer image using below link to know about the callout.

在此输入图像描述

In word document, I can do the same using Spire.Doc library and writing below code:

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

Here is a minimal example. It creates a Panel subclass with a nested TextBox .

Note that you can't add controls to a PictureBox in the designer. Instead you can put it on top and use the Nest function to embed it in the PBox..

(I use a trick to simplify the drawing of the border: since shrinking a GraphicsPath is hard and I am too lazy to write out two of them, I draw the border twice and add a little offset. The effect is a shadow effect, which looks rather nice for a cheat..)

Here is the class in action:

在此输入图像描述

And here is the class code:

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

You can set the colors and the Font in the designer..

And the Nest function:

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

It is called from the Form where the controls sit: Nest(wedgeCallout1, pictureBox1);

Note that to save to callout with the pbox in one image you need to

  • Nest the callout in the PBox
  • Use pbox.DrawToBitmap
  • Temporarily set the backcolor to transparent

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM