3

現在の例

従業員用の ICard を生成しています。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));
            }

現在の出力は、画像の左側に表示されます。ここで、私の弦が箱から出るとどうなりますか。

固定サイズの箱に綴じたい。

画像の右側に例を示します。

4

2 に答える 2

5

ポイントの代わりに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);
}

そのような単純な :)

于 2012-03-16T11:11:00.967 に答える
2

私はあなたのコードにいくつかの変更を加え、2行をコメントしました-私はC:\Users\admin\Pictures\filename.jpg私のPCにファイルを持っていませんでした-それblankICardが無効にされた理由であり、そのようにRectangle

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));
        }
于 2012-03-16T10:37:34.343 に答える