c#像素和英寸转换
来源:互联网 发布:telnet 端口连接失败 编辑:程序博客网 时间:2024/04/30 15:03
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace advanced_drawing
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- using (Font rulerFont = new Font("MS Sans Serif", 100f))
- {
-
- float ruleFontHeight = rulerFont.SizeInPoints ;
- RectangleF rulerRect = new RectangleF(0,100f, 100f, ruleFontHeight);
- Graphics g = this.CreateGraphics();
- g.DrawRectangle(Pens.Black, InchesTopixels(rulerRect.X), InchesTopixels(rulerRect.Y), InchesTopixels(rulerRect.Width), InchesTopixels(rulerRect.Height));
- Font font = new Font("Arial", 12);
- Brush brush = System.Drawing.Brushes.Red;
- g.DrawString("zhuzhao", font, brush, rulerRect);
- }
- }
- public float InchesTopixels(float inches)
- {
- using (Graphics g = this.CreateGraphics())
- {
- return inches * g.DpiX;
- }
- }
- }
- }