在图像上写文字(指定宽度)

时间:2012-03-16 10:25:02

标签: c# .net system.drawing drawstring

current example

我正在为员工制作ICards。 我必须在ICard上写下员工的地址。

            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));
            }

电流输出显示在图像的左侧。 在这里,我的字符串开箱即用。

我想将它绑定在修复大小框中。

示例显示在图像的右侧。

2 个答案:

答案 0 :(得分:4)

使用Graphics.DrawString重载,而不是点Rectangle。这样你就可以将文本换行以适应指定的宽度。

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);
}

简单如下:)

答案 1 :(得分:2)

我对您的代码进行了一些更改,评论了2行 - 我的电脑上没有文件C:\Users\admin\Pictures\filename.jpg - 这就是blankICard被禁用的原因,Rectangle也是如此:< / p>

例如,您必须设置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));
        }