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

时间:2018-02-06 06:37:15

标签: c# dynamic graphics label picturebox

好的,开始吧。我正在尝试将动态可编辑,可添加和可移动的文本制作到图片框上。我做到了。

从图片框保存图像时,不会保存标签。我现在让它使用Graphics将标签绘制为字符串。然而,它只将最后修改/添加/编辑的标签绘制到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);
            }
        }
    }

1 个答案:

答案 0 :(得分: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);
}