-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