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



好吧,开始。我正在尝试将动态的可编辑,可添加和可移动的文本在图片框上进行。我有工作。

从图像框中保存图像时,它不会保存标签。我现在得到了使用图形的字符串绘制标签。但是,它仅将最后一个修改的/添加/编辑的标签绘制到图片框。我迷路了。

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

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

最新更新