CODEa所:
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
graphics.DrawString( stringB,
new Font("Tahoma", 14), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
CodeB:
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n"+stringB,
new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
我需要在一个盒子里画2个字符串。具有下划线样式的StringA,而StringB不具有。
CodeB几乎实现了我的目标,但stringA
和stringB
共享相同的样式。所以我使用CodeA进行了测试,但是使用它的程序是两个字符串相互重叠。我可以知道
答案 0 :(得分:0)
codeA的问题在于stringA和stringB都是在完全相同的位置绘制的。
graphics.DrawString
将字符串转换为图像并将其打印在纸张上。
" \ n"一旦字符串变成图像,它就没有任何意义。它不会被打印,也不会创建新的一行。事实上,没有"线"在纸上。只是形象。
你需要给stringB不同的位置。使用Graphics.MeasureString (String, Font)
来衡量stringA的大小,然后根据结果调整stringB的位置。
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Font strFontA = new Font("Tahoma", 14, FontStyle.Underline);//Font used by stringA
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
strFont_A, Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
SizeF stringSizeA = new SizeF();
stringSizeA = Graphics.MeasureString(stringA, strFont_A);//Measuring the size of stringA
graphics.DrawString(stringB,
new Font("Tahoma", 14), Brushes.Black,
new RectangleF(0, stringSizeA.Height, photoWidth, photoHeight - stringSizeA.Height), strFormat);