首页 技术 正文
技术 2022年11月6日
0 收藏 600 点赞 888 浏览 1479 个字

从目前所掌握的资料来看,c#程序中将毫米转换像素的方法无非两种:

第一种:

   1: /// <summary>

   2: /// 以毫米为单位的显示宽度

   3: /// </summary>

   4: const int HORZSIZE = 4;

   5: /// <summary>

   6: /// 以像素为单位的显示宽度 0~65535

   7: /// </summary>

   8: const int HORZRES = 8;

   9: const int LOGPIXELSX = 88;

  10: const int LOGPIXELSY = 90;

  11: public static double MillimetersToPixelsWidth(IntPtr handle, double length) //length是毫米,1厘米=10毫米

  12: {

  13:     System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(handle);

  14:     IntPtr hdc = g.GetHdc();

  15:     int width = GetDeviceCaps(hdc, HORZSIZE);     // HORZRES 

  16:     int pixels = GetDeviceCaps(hdc, HORZRES);     // BITSPIXEL

  17:     g.ReleaseHdc(hdc);

  18:     return (((double)pixels / (double)width) * (double)length);

  19: }

  20:  

  21: [System.Runtime.InteropServices.DllImport("gdi32.dll")]

  22: private static extern int GetDeviceCaps(IntPtr hdc, int Index);

此种方法计算的值与实际刻度相比:10mm=实际刻度8mm

以此技术的程序:桌面刻度尺

技术文章引用:http://hi.baidu.com/kingcham/item/b3653ce0c69756216dabb8cd

在文章中所说的

GDI中有一个函数是GetDeviceCaps(),可以获取一些关于设备的一些属性,如HORZSIZE/HORZRES/LOGPIXELSX等。
以上三者的关系通常满足:HORZSIZE = 25.4 * HORZRES/LOGPIXELSX

但是在程序中却无法满足该条件。

第二种:

   1: /// <summary>

   2: /// 1英寸=25.4毫米

   3: /// </summary>

   4: const double millimererTopixel = 25.4;

   5:  

   6: public static double MillimeterToPixel(IntPtr handle, double length) //length是毫米,1厘米=10毫米

   7: {

   8:    System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();

   9:    System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(handle);

  10:  

  11:    //1英寸=25.4mm=96DPI,那么1mm=96/25.4DPI

  12:    return (((double)g.DpiX / millimererTopixel) * (double)length);

  13: }

此种方法是根据网上的换算关系得来的。得到的值与实际刻度相比:180mm=实际刻度185mm

以此技术的程序:夏克屏幕刻度尺

以目前而言,还无法准确的进行转换,从而绘制标准刻度尺。如果各位有更好的方法,还望提示下,先谢谢了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,154
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,623
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,465
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,239
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,042