code_junkie

Height of string measured with MeasureString in PrintPreviewDialog differs from that in actual printing

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. Print();
  4. }
  5.  
  6. public void Print()
  7. {
  8. PrintDocument my_doc = new PrintDocument();
  9. my_doc.PrintPage += new PrintPageEventHandler(this.PrintPage);
  10.  
  11. PrintPreviewDialog my_preview = new PrintPreviewDialog();
  12. my_preview.Document = my_doc;
  13.  
  14. my_preview.ShowDialog();
  15.  
  16. my_doc.Dispose();
  17. my_preview.Dispose();
  18. }
  19.  
  20. private void PrintPage(object sender,
  21. System.Drawing.Printing.PrintPageEventArgs e)
  22. {
  23. e.Graphics.PageUnit = GraphicsUnit.Pixel;
  24.  
  25. string s = "String height is ";
  26.  
  27. SizeF h = e.Graphics.MeasureString(s, new Font("Arial", 24));
  28.  
  29. e.Graphics.DrawString(s + Convert.ToString(h.Height),
  30. new Font("Arial", 24), new SolidBrush(Color.Black), 1, 1);
  31. }
  32.  
  33. SizeF hT = TextRenderer.MeasureText(s, new Font("Arial", 24));
Add Comment
Please, Sign In to add comment