c#像素和英寸转换

来源:互联网 发布:telnet 端口连接失败 编辑:程序博客网 时间:2024/04/30 15:03
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace advanced_drawing
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.         private void button1_Click(object sender, EventArgs e)
  17.         {
  18.             using (Font rulerFont = new Font("MS Sans Serif", 100f))
  19.             {
  20.                 
  21.                 float ruleFontHeight = rulerFont.SizeInPoints ;
  22.                 RectangleF rulerRect = new RectangleF(0,100f, 100f, ruleFontHeight);
  23.                 Graphics g = this.CreateGraphics();
  24.                 g.DrawRectangle(Pens.Black, InchesTopixels(rulerRect.X), InchesTopixels(rulerRect.Y), InchesTopixels(rulerRect.Width), InchesTopixels(rulerRect.Height));
  25.                 Font font = new Font("Arial", 12);
  26.                 Brush brush = System.Drawing.Brushes.Red;
  27.                 g.DrawString("zhuzhao", font, brush, rulerRect); 
  28.             }
  29.         }
  30.         public float InchesTopixels(float inches)
  31.         {
  32.             using (Graphics g = this.CreateGraphics())
  33.             {
  34.                 return inches * g.DpiX;
  35.             }
  36.         }
  37.     }
  38. }
原创粉丝点击