2012-03-16 60 views
2

current example在圖像上書寫文字(指定寬度)

我正在爲員工生成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)); 
      } 

電流輸出被示出在圖像的左側。 這裏發生了什麼,我的字符串出箱。

我想在修正大小框中將其綁定。

示例顯示在圖像的右側。

+0

使用此重載拉帶:http://msdn.microsoft.com/en-us/library/21kdfbzs.aspx – Rotem 2012-03-16 10:36:10

回答

3

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

這麼簡單:)

2

我做了一些修改代碼,評論2線 - 我沒有足夠的文件C:\Users\admin\Pictures\filename.jpg在我的電腦 - 這就是爲什麼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)); 
     } 
+0

寬度工作fine..but休息的數據已被丟棄,我希望該數據(其餘數據)應該在下行。 – 2012-03-16 11:42:45

+0

對不起,你在說什麼樣的數據?在這個例子中,我將maxWidth設置爲30,但是如果你增加可能你會發現字符串的其餘部分。 PS:我無法使用FreightSans進行測試,因爲我的電腦上沒有這種字體。我用ARIAL測試過。 – ClayKaboom 2012-03-16 12:33:51