2013-05-13 81 views
-1

我是DotNet的新手,如果我用磅秤進行測量,我想要打印一個寬度爲20mm,高度爲8mm的矩形。我也想在矩形中間準確地打印文本。任何人都可以告訴我如何實現這一目標?如何在c#中使用精確大小打印矩形?


我真的很抱歉沒有被提前清楚。我曾嘗試使用「PageUnits」工作正常。不過,我的利潤率有問題。

如果我使用打印機「HP LaserJet P2035n」,我可以打印正確的頁邊距(左側8.8mm和頂部22mm)。如果使用「Canon iR2020 PCL5e」進行打印,我會得到不正確的頁邊距(左側8.1mm和頂部8.0mm),我應該留下8.8mm左右和22mm上邊距。有人能解釋我在哪裏做錯了嗎?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Drawing.Printing; 
namespace ConsoleApplication6 
{ 
    class DrawShape 
    { 
     public static void DrawRec() 
     { 
      PrintDocument doc = new PrintDocument(); 
      doc.PrintPage += doc_PrintPage; 
      doc.Print(); 
     } 

     static void doc_PrintPage(object sender, PrintPageEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      PageSettings PageSet = new PageSettings(); 
      float MarginX = PageSet.PrintableArea.X; 
      float MarginY = PageSet.PrintableArea.Y; 
      float x = (float)(8.8-((MarginX/100)*25.4)); 
      float y = (float)(22-((MarginY/100)*25.4)); 
      g.PageUnit = GraphicsUnit.Millimeter; 
      g.DrawRectangle(Pens.Black, x, y, 20, 8); 

     } 
    } 
} 
+0

你嘗試過什麼嗎? – tnw 2013-05-13 18:29:47

回答

0

你可能要開始與此:

private void button1_Click(object sender, EventArgs e) 
    { 
     using (Graphics formGraphics = this.CreateGraphics()) 
     { 
      formGraphics.PageUnit = GraphicsUnit.Millimeter; 
      formGraphics.DrawRectangle(Pens.Blue, 0, 0, 20, 80); 
     } 
    } 

然後,您可以使用的DrawString在Graphics對象繪製的矩形內的文字。

+0

感謝您的回覆......我會試試這個 – Ramana 2013-05-13 19:42:20