C#中怎样把 像素值 转换为 毫米值? 取决于显示器和分辨率设定一般而言是96dpi(每英寸96个点) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int mm=0;mm=this.Button1.Width/96*25.4; int mm=0;int dpp;mm=this.Button1.Width/dpp*25.4; dpi=像素/英寸1英寸=25.4毫米知道了吧? 从毫米转换为象素的方法Point MMConv(Graphics grfx,Point pointf){pointf.X*=grfx.DpiX/25.4f;pointf.Y*=grfx.DpiY/25.4f;return pointf;}向这个方法传递的点是您所希望的毫米单位。将这个点除以25.4可以将它转换成英寸。将结果乘以没英寸的点数所表示的分辨率就会将它转换成象素。由象素转换成毫米,反过来就可以了 private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g =e.Graphics; //已毫米绘制。 g.PageUnit = GraphicsUnit.Millimeter; //Console.WriteLine((int)e.PageSettings.PrinterResolution.Y); Rectangle rect = button1.Bounds; RectangleF rf = new RectangleF(16,25,(rect.Width/3.8f),(rect.Height/3.8f)); Pen pen= Pens.Black.Clone() as Pen; pen.Width=0.27f; //画比较线 g.DrawLine(pen,16,0,16,1000); g.DrawLine(pen,rf.Right,0,rf.Right,1000); //使用换算完的 范围 g.DrawRectangle(pen,rf.X,rf.Y,rf.Width,rf.Height); //用系统绘制一个button /* 其实就是这个api 本api 会根据 当前绘图环境 自己调整 * 所以传一个正常的 button 的大小即可,没有比这个在标准的了。 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern bool DrawFrameControl(HandleRef hDC, ref NativeMethods.RECT rect, int type, int state); */ ControlPaint.DrawButton(g,rect,ButtonState.Normal); StringFormat sf =new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment =StringAlignment.Center; g.DrawString(button1.Text,button1.Font,Brushes.Black,rf,sf); sf.Dispose(); /* 根据导航线得出结论,就是 3.8 */ } private void button1_Click(object sender, System.EventArgs e) { PrintPreviewDialog pdlg =new PrintPreviewDialog(); PrintDocument pdoc = new PrintDocument(); pdlg.Document=pdoc; pdoc.PrintPage +=new PrintPageEventHandler(pdoc_PrintPage); pdlg.ShowDialog(); } 我这就回去试验一下,用API也可以的,不过麻烦 winform程序中怎么进行加密? 字符编码转换 如何用c#创建bat文件,并写入内容 如何在SQL2005中查看表之间的关系图? xml in C#请教 Properties的问题 Menu高亮问题 WinForm如何设置拖动改变窗口的高度(宽度不能改变) datarid添加固定列的问题 关于安装的问题 安装程序完成后怎样自动运行命令? 巨难!!以前问过没有解决.C#使用COM对象时,调用COM的函数,其默认参数怎样传递??
mm=this.Button1.Width/96*25.4;
int dpp;
mm=this.Button1.Width/dpp*25.4;
1英寸=25.4毫米
知道了吧?
Point MMConv(Graphics grfx,Point pointf)
{
pointf.X*=grfx.DpiX/25.4f;
pointf.Y*=grfx.DpiY/25.4f;
return pointf;
}
向这个方法传递的点是您所希望的毫米单位。将这个点除以25.4可以将它转换成英寸。将结果乘以没英寸的点数所表示的分辨率就会将它转换成象素。
由象素转换成毫米,反过来就可以了
{
Graphics g =e.Graphics;
//已毫米绘制。
g.PageUnit = GraphicsUnit.Millimeter;
//Console.WriteLine((int)e.PageSettings.PrinterResolution.Y);
Rectangle rect = button1.Bounds;
RectangleF rf = new RectangleF(16,25,(rect.Width/3.8f),(rect.Height/3.8f)); Pen pen= Pens.Black.Clone() as Pen;
pen.Width=0.27f;
//画比较线
g.DrawLine(pen,16,0,16,1000);
g.DrawLine(pen,rf.Right,0,rf.Right,1000);
//使用换算完的 范围
g.DrawRectangle(pen,rf.X,rf.Y,rf.Width,rf.Height);
//用系统绘制一个button
/* 其实就是这个api 本api 会根据 当前绘图环境 自己调整
* 所以传一个正常的 button 的大小即可,没有比这个在标准的了。
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool DrawFrameControl(HandleRef hDC, ref NativeMethods.RECT rect, int type, int state);
*/
ControlPaint.DrawButton(g,rect,ButtonState.Normal);
StringFormat sf =new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment =StringAlignment.Center;
g.DrawString(button1.Text,button1.Font,Brushes.Black,rf,sf);
sf.Dispose(); /*
根据导航线得出结论,就是 3.8
*/
} private void button1_Click(object sender, System.EventArgs e)
{
PrintPreviewDialog pdlg =new PrintPreviewDialog();
PrintDocument pdoc = new PrintDocument();
pdlg.Document=pdoc;
pdoc.PrintPage +=new PrintPageEventHandler(pdoc_PrintPage);
pdlg.ShowDialog();
}