C# 在图像上写入文本(指定宽度)
我正在为员工生成iCard。 我必须在ICard上写下员工的地址C# 在图像上写入文本(指定宽度),c#,.net,system.drawing,drawstring,C#,.net,System.drawing,Drawstring,我正在为员工生成iCard。 我必须在ICard上写下员工的地址 Image blankICard = Image.FromFile(@"C:\Users\admin\Pictures\filename.jpg"); Bitmap outputImage = new Bitmap(blankICard.Width, blankICard.Height, System.Drawing.Imaging.PixelFormat.Format32bppA
Image blankICard = Image.FromFile(@"C:\Users\admin\Pictures\filename.jpg");
Bitmap outputImage = new Bitmap(blankICard.Width, blankICard.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.SolidBrush b = new SolidBrush(Color.FromArgb(255, 88, 89, 91));
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawImage(blankICard, new Rectangle(0, 0, blankICard.Width, blankICard.Height),
new Rectangle(new Point(), blankICard.Size), GraphicsUnit.Pixel);
Font stringFont = new Font("FreightSans Medium", 20, FontStyle.Regular);
string address = "Address goes here";
graphics.DrawString(address, new Font("FreightSans Medium", 20, FontStyle.Regular), b, new Point(621, 234));
graphics.DrawString("Employee Code:12345678", new Font("FreightSans Medium", 26, FontStyle.Regular), b, new Point(350, 407));
}
电流输出显示在图像的左侧。
这里发生了什么,我的字符串出了框
我想把它绑在固定大小的盒子里
示例如图右侧所示。我对您的代码做了一些更改,注释了两行内容-我的电脑上没有文件
C:\Users\admin\Pictures\filename.jpg
,这就是为什么blankICard
被禁用,它的矩形也被禁用的原因:
例如,必须设置maxWidth
,才能包装员工代码
// Image blankICard = Image.FromFile(@"C:\Users\admin\Pictures\filename.jpg");
int width = 500;
int height = 500;
Bitmap outputImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.SolidBrush b = new SolidBrush(Color.FromArgb(255, 88, 89, 91));
SolidBrush blackBrush = new SolidBrush(Color.Black);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawRectangle( new Pen(blackBrush) ,new Rectangle(0, 0, width, height));
// new Rectangle(new Point(), blankICard.Size), GraphicsUnit.Pixel);
Font stringFont = new Font("FreightSans Medium", 20, FontStyle.Regular);
string address = "Address goes here";
string employeeCode = "Employee Code:12345678";
int maxWidth = 30;
SizeF sf = graphics.MeasureString(employeeCode,
new Font(new FontFamily("FreightSans Medium"), 26), maxWidth);
graphics.DrawString(address, new Font("FreightSans Medium", 20, FontStyle.Regular), b, new Point(0, 0));
graphics.DrawString(employeeCode,
new Font(new FontFamily("FreightSans Medium"), 26), Brushes.Black,
new RectangleF(new PointF(0, 25), sf),
StringFormat.GenericTypographic);
//graphics.DrawString(, new Font("FreightSans Medium", 26, FontStyle.Regular), b, new Point(10, 20));
}
使用Graphics.DrawString
重载,以矩形
代替点。这样,您将在指定的宽度内包装文本
using (Graphics graphics = Graphics.FromImage(outputImage)){
// Draw whatever you need first
// ....
// Create font...
graphics.DrawString(employeeCode, font, Brushes.Black,
new Rectangle(0, 25, maxWidth, maxHeight);
}
很简单:)使用这个超负荷的抽绳:宽度很好。但是其余的数据已经被丢弃了,我希望数据(其余的数据)应该放在下一行。对不起,你说的是什么类型的数据?在本例中,我将maxWidth设置为30,但如果增加,则可能会找到字符串的其余部分。PS:我不能用FreightSans进行测试,因为我的电脑上没有这种字体。我用ARIAL进行了测试。