-1
我得出基于文本(_label)图像C#调整PictureBox中生成的图像
public Image getImage()
{
PointF initialLocation = new PointF(0.1f, 0.1f);
//Bitmap b = new Bitmap(130, 50);
Graphics g = Graphics.FromImage(new Bitmap(1,1));
System.Drawing.Font font = new System.Drawing.Font("Arial", 16, FontStyle.Regular);
SizeF bounds = g.MeasureString(_label, font, new PointF(0,0), new StringFormat(StringFormatFlags.MeasureTrailingSpaces));
Bitmap b = new Bitmap((int)bounds.Width, (int)bounds.Height);
//b.MakeTransparent();
using (Graphics graphics = Graphics.FromImage(b))
{
//using (Font arialFont = new Font("Arial", 16))
{
graphics.DrawString(_label, font, Brushes.White, initialLocation);
}
}
Image image = b;
return image;
}
这绘制的图像的文本需要多久的大小。例如,在这种情况下,“LabelTest”将是Image.Width = 103和Image.Height = 26.我也希望能够更新(设置)文本并在屏幕上更新它。当我文本更改为“LabelTest123456789”,并再次调用的getImage()的方法,所述Image.Width变为213
在父类我然后设置上面提到的PictureBox的Image:
public partial class Widget : System.Windows.Forms.PictureBox
.. .. .. 然后
else if (t == Widget.WidgetType.LABEL)
{
**base.Image = image;**
base.BackColor = Color.Transparent;
}
一个OnPaint方法被调用来绘制在屏幕上:
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
在这个'onPaint'方法中,ClipRectangle的值仍然具有从第一次设置时的大小的属性(103,26),我预计这将使用新的大小(213,26)。
目标是根据一串文字制作图像,我也希望能够更新字符串并重绘更新的字符串。
非常感谢提前!
您未显示如何更新文本,然后更改为新图像。另外,PictureBox的SizeMode()是什么设置的? – 2014-12-05 16:20:13
你不需要自己调用'OnPaint()'。只需设置一个新的'Image'值就可以使'PictureBox'以可视方式进行自我更新。请参阅http://stackoverflow.com/help/mcve和http://stackoverflow.com/help/how-to-ask,获取有关如何编辑问题的建议,以便清楚并且可以回答。 – 2014-12-05 18:42:50
谢谢彼得,这似乎有帮助! – 2014-12-08 08:35:33