这个问题很简单,假设你有一个形状(可以说是一个矩形)和一个文本(例如“hello”),所以它会将文本全部写入矩形的边界,适合,例如:如何在形状中绘制文字? C#
hello hello hello hello
hello hello
hello hello
hello hello hello hello
为了做到这一点我假设你需要使用图形变量,我只是不知道该怎么做。
一种用于在一个位图对象绘制的字符串的代码:
Bitmap tempp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(tempp);
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format);
g.Flush();
预先感谢。
对于第二点意见:
hello hello hello
hel llo
hel llo
hello hello hello
一旦你有一个字符串,图像的大小,计算最终的矩形的总规模,并把几个字符串映像副本,那么,每一个副本在其自己的坐标。也许[这个链接](http://stackoverflow.com/questions/1478022/c-sharp-get-a-controls-position-on-a-form)可以帮助你。 –
矩形的大小如何?如果连续有四个“你好”太大,而五个太小则太大。你是否调整矩形的大小?或者你调整文本之间的间距? – Anthony
@Anthony像这样做:我会编辑线程,因为我不能在这里显示代码。 –