【问题标题】:Writing dynamic label to picturebox image only drawing the last modified label将动态标签写入图片框图像仅绘制最后修改的标签
【发布时间】:2018-02-06 15:30:54
【问题描述】:

好的,开始吧。我正在尝试在图片框上制作动态可编辑、可添加和可删除的文本。我得到了它的工作。

从图片框中保存图像时,不会保存标签。我现在可以使用 Graphics 将标签绘制为字符串。然而,它只将最后修改/添加/编辑的标签绘制到图片框。我迷路了。

这是我绘制标签并保存它们的代码:

if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string ext = Path.GetExtension(sfd.FileName);
                switch (ext)
                {
                    case ".jpg":
                        format = ImageFormat.Jpeg;
                        break;
                    case ".bmp":
                        format = ImageFormat.Bmp;
                        break;
                }
                Bitmap bmp = new Bitmap(pictureBox1.Image);

                RectangleF rectf = new RectangleF(70, 90, 90, 50);

                Graphics g = Graphics.FromImage(bmp);

                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                g.Flush();
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                SolidBrush brush = new SolidBrush(label.ForeColor);

                for (int i = 0; i < n; i++)
                { 
                    g.DrawString(label.Text, label.Font, brush, label.Location);
                    label.SelectNextControl(label, false, false, true, false);
                }

                pictureBox1.Image = bmp;
                pictureBox1.Image.Save(sfd.FileName, format);
            }

这里是定义和创建标签的地方:

label = new CustomLabel();
label.Name = "" + n;
label.Location = new Point(newTextbox.Location.X, newTextbox.Location.Y);
label.Text = newTextbox.Text;
label.Font = new Font("Verdana", fontSize);
label.BackColor = Color.Transparent; 

label.ForeColor = textColor;
label.AutoSize = true;
label.Visible = true;
newTextbox.Visible = false;
newTextbox.Dispose();

pictureBox1.Controls.Add(label);
TextSelected = false;
label.DoubleClick += new System.EventHandler(this.label_DoubleClick);
label.MouseDown += new MouseEventHandler(this.label_MouseDown);
label.MouseUp += new MouseEventHandler(this.MouseUp);
label.MouseMove += new MouseEventHandler(this.MouseMove);
n++;

并且定义了n:

public int n = 1;

笔画添加到文本的位置:

public class CustomLabel : Label
    {
        public CustomLabel()
        {
            OutlineForeColor = Color.Black;
            OutlineWidth = 3;
        }
        public Color OutlineForeColor { get; set; }
        public float OutlineWidth { get; set; }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
            using (GraphicsPath gp = new GraphicsPath())
            using (Pen outline = new Pen(OutlineForeColor, OutlineWidth)
            { LineJoin = LineJoin.Round })
            using (StringFormat sf = new StringFormat())
            using (Brush foreBrush = new SolidBrush(ForeColor))
            {
                gp.AddString(Text, Font.FontFamily, (int)Font.Style,
                    Font.Size, ClientRectangle, sf);
                e.Graphics.ScaleTransform(1.3f, 1.35f);
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                e.Graphics.DrawPath(outline, gp);
                e.Graphics.FillPath(foreBrush, gp);
            }
        }
    }

【问题讨论】:

  • 上一个有 30+ 的浏览量并且没有答案。我需要解决这个问题。我尝试了多种失败的“解决方案”。我删除了旧帖子。

标签: c# dynamic graphics label picturebox


【解决方案1】:

问题出在你的 for 循环中:

for (int i = 0; i < n; i++)
{ 
    g.DrawString(label.Text, label.Font, brush, label.Location);
    label.SelectNextControl(label, false, false, true, false);
}

这里label 永远不会改变,所以你只是在绘制相同的标签n 次。而且我不知道SelectNextControl 做了什么。

我建议循环遍历图片框中的控件:

foreach (var customLabel in pictureBox1.Controls.OfType<CustomLabel>()) {
    g.DrawString(customLabel.Text, customLabel.Font, brush, customLabel.Location);
}

【讨论】:

  • 太棒了!这样可行!现在,还有一个问题。我如何让它在不重叠的情况下删除标签?如,它写入图像但标签仍然存在,因此它是相同文本的两层。
  • 在 foreach 循环之后执行pictureBox1.Controls.Clear()。 @Disloyalg
  • 现在它正在删除除第一个添加之外的所有内容,而不是添加笔画。
  • @Disloyalg 你的意思是输出图像上没有文字吗?而且图片框上还有一个标签?
  • 输出图像具有应有的所有文本,并且标签仍在图片框上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多