将动态标签写入图片框图像仅绘制最后修改的标签

Writing dynamic label to picturebox image only drawing the last modified label

好的,开始吧。我正在尝试将动态可编辑、可添加和可删除的文本添加到图片框上。我成功了。

从图片框保存图像时,不会保存标签。我现在可以使用 Graphics 将标签绘制为字符串。然而,它只将最后一个 modified/added/edited 标签绘制到 pictureBox。我迷路了。

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

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

问题出在你的 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);
}