DrawFunction.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OTSIncAReportGraph.Class;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11. using OpenCvSharp;
  12. namespace OTSIncAReportGraph.Class
  13. {
  14. /// <summary>
  15. /// 相关图表绘制函数统一封装类
  16. /// </summary>
  17. static class DrawFunction
  18. {
  19. [DllImport("gdi32.dll")]
  20. public static extern System.UInt32 GetPixel(IntPtr hdc, int xPos, int yPos);
  21. /// <summary>
  22. /// 将一个byte的数组转换为8bit灰度位图
  23. /// </summary>
  24. /// <param name="data">数组</param>
  25. /// <param name="width">图像宽度</param>
  26. /// <param name="height">图像高度</param>
  27. /// <returns>位图</returns>
  28. public static Bitmap ToGrayBitmap(byte[] data, int width, int height)
  29. {
  30. //// 申请目标位图的变量,并将其内存区域锁定
  31. Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  32. //// BitmapData这部分内容 需要 using System.Drawing.Imaging;
  33. BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
  34. ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  35. //// 获取图像参数
  36. // 扫描线的宽度
  37. //int stride = bmpData.Stride;
  38. // 显示宽度与扫描线宽度的间隙
  39. int offset = width - width;
  40. // 获取bmpData的内存起始位置
  41. IntPtr iptr = bmpData.Scan0;
  42. // 用stride宽度,表示这是内存区域的大小
  43. int scanBytes = width * height;
  44. //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
  45. int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
  46. byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
  47. for (int x = 0; x < height; x++)
  48. {
  49. //// 下面的循环节是模拟行扫描
  50. for (int y = 0; y < width; y++)
  51. {
  52. pixelValues[posScan++] = data[posReal++];
  53. }
  54. posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
  55. }
  56. //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
  57. System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
  58. bmp.UnlockBits(bmpData); // 解锁内存区域
  59. //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
  60. ColorPalette tempPalette;
  61. using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
  62. {
  63. tempPalette = tempBmp.Palette;
  64. }
  65. for (int i = 0; i < 256; i++)
  66. {
  67. tempPalette.Entries[i] = Color.FromArgb(i, i, i);
  68. }
  69. bmp.Palette = tempPalette;
  70. //// 算法到此结束,返回结果
  71. return bmp;
  72. }
  73. /// <summary>
  74. /// *有BUG*将图片缩放到目标宽高,该方法有问题,不应该继续使用,图像申缩存在不规则问题
  75. /// </summary>
  76. /// <param name="sourceImage"></param>
  77. /// <param name="targetWidth"></param>
  78. /// <param name="targetHeight"></param>
  79. /// <returns></returns>
  80. public static Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
  81. {
  82. int width;//图片最终的宽
  83. int height;//图片最终的高
  84. try
  85. {
  86. System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
  87. Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
  88. Graphics g = Graphics.FromImage(targetPicture);
  89. g.Clear(Color.White);
  90. //计算缩放图片的大小
  91. if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
  92. {
  93. width = targetWidth;
  94. height = (width * sourceImage.Height) / sourceImage.Width;
  95. }
  96. else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
  97. {
  98. height = targetHeight;
  99. width = (height * sourceImage.Width) / sourceImage.Height;
  100. }
  101. else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
  102. {
  103. width = sourceImage.Width;
  104. height = sourceImage.Height;
  105. }
  106. else
  107. {
  108. width = targetWidth;
  109. height = (width * sourceImage.Height) / sourceImage.Width;
  110. if (height > targetHeight)
  111. {
  112. height = targetHeight;
  113. width = (height * sourceImage.Width) / sourceImage.Height;
  114. }
  115. }
  116. g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
  117. g.Dispose();
  118. return targetPicture;
  119. }
  120. catch (Exception ex)
  121. {
  122. string str = ex.ToString();
  123. }
  124. return null;
  125. }
  126. /// <summary>
  127. /// 根据传入的宽和高,取得当前对应显示的4:3的标准分辨率
  128. /// </summary>
  129. /// <returns></returns>
  130. public static Rectangle Get43ScaleResolving(int in_width, int in_height)
  131. {
  132. Rectangle rect_resolving = new Rectangle();
  133. int width_min = 0;
  134. int height_min = 0;
  135. int width_max = 0;
  136. int height_max = 0;
  137. width_min = 1600;
  138. height_min = 1200;
  139. width_max = 2048;
  140. height_max = 1536;
  141. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  142. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  143. width_min = 1400;
  144. height_min = 1050;
  145. width_max = 1600;
  146. height_max = 1200;
  147. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  148. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  149. width_min = 1280;
  150. height_min = 1024;
  151. width_max = 1400;
  152. height_max = 1050;
  153. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  154. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  155. width_min = 1024;
  156. height_min = 768;
  157. width_max = 1280;
  158. height_max = 1024;
  159. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  160. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  161. width_min = 800;
  162. height_min = 600;
  163. width_max = 1024;
  164. height_max = 768;
  165. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  166. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  167. width_min = 640;
  168. height_min = 480;
  169. width_max = 800;
  170. height_max = 600;
  171. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  172. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  173. width_min = 320;
  174. height_min = 240;
  175. width_max = 640;
  176. height_max = 480;
  177. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  178. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  179. width_min = 0;
  180. height_min = 0;
  181. width_max = 320;
  182. height_max = 240;
  183. if ((in_width >= width_min && in_width < width_max) || (in_height >= height_min && in_height < height_max))
  184. rect_resolving = new Rectangle(0, 0, width_min, height_min);
  185. return rect_resolving;
  186. }
  187. /// <summary>
  188. /// [该方法效果正确,但速度上慢,淘汰掉],上下翻转
  189. /// </summary>
  190. /// <param name="mybm">原始图片</param>
  191. /// <param name="width">原始图片的长度</param>
  192. /// <param name="height">原始图片的高度</param>
  193. public static Bitmap RevPicUD(Bitmap mybm, int width, int height)
  194. {
  195. Bitmap bm = new Bitmap(width, height);
  196. int x, y, z;
  197. Color pixel;
  198. for (x = 0; x < width; x++)
  199. {
  200. for (y = height - 1, z = 0; y >= 0; y--)
  201. {
  202. pixel = mybm.GetPixel(x, y);//获取当前像素的值
  203. bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
  204. }
  205. }
  206. return bm;
  207. }
  208. /// <summary>
  209. /// 实现计算的方法,就是输出个数字,然后向上寻找倍数,然后返回来根
  210. /// </summary>
  211. /// <param name="in_number"></param>
  212. /// <returns></returns>
  213. public static int GetSquareRoot(int in_number)
  214. {
  215. int i_increase = 1;
  216. while (true)
  217. {
  218. //防止死循环
  219. if (i_increase > 10000)
  220. return i_increase;
  221. int ls_i = i_increase * i_increase;
  222. if (in_number > ls_i)
  223. {
  224. //如果当前的数值已经大于平方数的话,那就继续下一次的平方
  225. i_increase++;
  226. continue;
  227. }
  228. else
  229. {
  230. //如果已经小于平方数的话,那就说明已经找到了平方根,返回
  231. return i_increase;
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// 根据传入的数字返回对应的颜色,范围1-17种颜色,挑出来比较谈的颜色为对比效果好看而挑选
  237. /// </summary>
  238. /// <param name="number"></param>
  239. /// <returns></returns>
  240. public static Color GetColorByNumber(int number)
  241. {
  242. Color ret_color = new Color();
  243. switch (number)
  244. {
  245. case 1:
  246. ret_color = Color.Blue;
  247. break;
  248. case 2:
  249. ret_color = Color.Brown;
  250. break;
  251. case 3:
  252. ret_color = Color.LimeGreen;
  253. break;
  254. case 13:
  255. ret_color = Color.Cyan;
  256. break;
  257. case 4:
  258. ret_color = Color.DarkBlue;
  259. break;
  260. case 5:
  261. ret_color = Color.Red;
  262. break;
  263. case 6:
  264. ret_color = Color.SaddleBrown;
  265. break;
  266. case 7:
  267. ret_color = Color.DimGray;
  268. break;
  269. case 8:
  270. ret_color = Color.Navy;
  271. break;
  272. case 9:
  273. ret_color = Color.Peru;
  274. break;
  275. case 10:
  276. ret_color = Color.Red;
  277. break;
  278. case 11:
  279. ret_color = Color.SeaGreen;
  280. break;
  281. case 12:
  282. ret_color = Color.MintCream;
  283. break;
  284. case 14:
  285. ret_color = Color.PaleTurquoise;
  286. break;
  287. case 15:
  288. ret_color = Color.SeaShell;
  289. break;
  290. case 16:
  291. ret_color = Color.Snow;
  292. break;
  293. case 17:
  294. ret_color = Color.WhiteSmoke;
  295. break;
  296. default:
  297. ret_color = Color.White;
  298. break;
  299. }
  300. return ret_color;
  301. }
  302. public static Color GetColorBySTDTypeIDForBSEAndSorImage(string in_partcolor, int in_stdtypeid)
  303. {
  304. Color ret_c = new Color();
  305. if (in_stdtypeid < 1000)
  306. {
  307. ret_c = GetColorByEnum(in_stdtypeid);
  308. }
  309. else if (in_stdtypeid >= 1000)
  310. {
  311. //大于等于1000,并且小于10000时,使用用户标准库来分析夹杂物名称
  312. if (!in_partcolor.Contains("#"))
  313. {
  314. ret_c = DrawFunction.colorHx16toRGB("#" + in_partcolor);//接收必须是#000000的格式
  315. }
  316. else
  317. {
  318. ret_c = DrawFunction.colorHx16toRGB(in_partcolor);//接收必须是#000000的格式
  319. }
  320. }
  321. return ret_c;
  322. }
  323. public static Color GetColorByEnum(int STDID)
  324. {
  325. Color ret_c = new Color();
  326. switch (STDID)
  327. {
  328. case -1:
  329. //INVALID = -1,
  330. //stdName = "无效颗粒";
  331. ret_c = Color.Black;
  332. break;
  333. case 0:
  334. //small = 0;
  335. //stdName = "过小颗粒";
  336. ret_c = Color.Black;
  337. break;
  338. case 1:
  339. //OVERSIZE = 1,
  340. //stdName = "过大颗粒";
  341. ret_c = Color.Black;
  342. break;
  343. case 2:
  344. //AVE_GRAY_NOT_INRANRE = 2,
  345. //stdName = "亮度不在分析范围内的颗粒";
  346. ret_c = Color.Black;
  347. break;
  348. case 3:
  349. //SEARCH_X_RAY = 3,
  350. //stdName = "不进行搜索x-ray分析的颗粒";
  351. ret_c = Color.Black;
  352. break;
  353. case 4:
  354. //LOW_COUNT = 4,
  355. //stdName = "低x-ray计数颗粒";
  356. ret_c = Color.Black;
  357. break;
  358. case 5:
  359. //NO_INTEREST_ELEMENTS = 5,
  360. //stdName = "不含分析元素的颗粒";
  361. ret_c = Color.Black;
  362. break;
  363. case 6:
  364. //ALAYSIS_X_RAY = 6,
  365. //stdName = "不进行x-ray分析的颗粒";
  366. ret_c = Color.Black;
  367. break;
  368. case 7:
  369. //NOT_IDENTIFIED = 7,
  370. //stdName = "未识别颗粒";
  371. ret_c = Color.Black;
  372. break;
  373. case 8:
  374. //NOT_IDENTIFIED = 8,
  375. //stdName = "未识别颗粒";
  376. ret_c = Color.Black;
  377. break;
  378. case 9:
  379. //NOT_IDENTIFIED = 9,
  380. //stdName = "未识别颗粒";
  381. ret_c = Color.Black;
  382. break;
  383. default:
  384. ret_c = Color.Black;
  385. break;
  386. }
  387. return ret_c;
  388. }
  389. /// <summary>
  390. /// 获取一个自绘模仿进度条的bitemap对象,传入进度的值,0-100之间
  391. /// </summary>
  392. /// <param name="process_value"></param>
  393. /// <returns></returns>
  394. public static Bitmap GetProcessBitmap(int process_value)
  395. {
  396. Bitmap bmp = new Bitmap(104, 30); //这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
  397. Graphics g = Graphics.FromImage(bmp);
  398. g.Clear(Color.White); //背景填白色
  399. //g.FillRectangle(Brushes.Red, 2, 2, this.Press, 26); //普通效果
  400. //填充渐变效果
  401. SolidBrush drawBrush = new SolidBrush(Color.Black);
  402. g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Point(30, 2), new System.Drawing.Point(30, 30), Color.Red, Color.Gray), 2, 2, process_value, 26);
  403. g.DrawString(process_value.ToString(), new Font("宋体", 9), drawBrush, 50, 8);
  404. return bmp;
  405. }
  406. /// <summary>
  407. /// 获取一个自绘模仿进度条的bitemap对象,传入进度的值,0-100之间[重载可以自定设置背景色]
  408. /// </summary>
  409. /// <param name="process_value"></param>
  410. /// <param name="back_color"></param>
  411. /// <returns></returns>
  412. public static Bitmap GetProcessBitmap(float process_value, Color back_color)
  413. {
  414. if (process_value < 0)
  415. process_value = 0;
  416. if (process_value > 100)
  417. process_value = 100;
  418. Bitmap bmp = new Bitmap(104, 30); //这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
  419. Graphics g = Graphics.FromImage(bmp);
  420. g.Clear(back_color); //背景填白色
  421. //g.FillRectangle(Brushes.Red, 2, 2, this.Press, 26); //普通效果
  422. //填充渐变效果
  423. SolidBrush drawBrush = new SolidBrush(Color.Black);
  424. g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Point(30, 2), new System.Drawing.Point(30, 30), Color.Red, Color.Gray), 2, 2, process_value, 26);
  425. g.DrawString(process_value.ToString(), new Font("宋体", 9), drawBrush, 50, 8);
  426. return bmp;
  427. }
  428. /// <summary>
  429. /// [颜色:16进制转成RGB]
  430. /// </summary>
  431. /// <param name="strColor">设置16进制颜色 [返回RGB]</param>
  432. /// <returns></returns>
  433. public static System.Drawing.Color colorHx16toRGB(string strHxColor)
  434. {
  435. try
  436. {
  437. if (strHxColor.Length == 0)
  438. {//如果为空
  439. return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
  440. }
  441. else
  442. {//转换颜色
  443. if (strHxColor.IndexOf('#') > -1)
  444. {
  445. //如果颜色格式是 #0000FF 格式的
  446. return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
  447. }
  448. else
  449. {
  450. //如果颜色格式是 0000FF 格式的
  451. return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
  452. }
  453. }
  454. }
  455. catch
  456. {//设为黑色
  457. return System.Drawing.Color.FromArgb(0, 0, 0);
  458. }
  459. }
  460. /// <summary>
  461. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  462. /// </summary>
  463. /// <param name="path"></param>
  464. /// <returns></returns>
  465. public static Bitmap ReadImageFile(string path)
  466. {
  467. if (!File.Exists(path))
  468. {
  469. return null;//文件不存在
  470. }
  471. FileStream fs = File.OpenRead(path); //OpenRead
  472. int filelength = 0;
  473. filelength = (int)fs.Length; //获得文件长度
  474. Byte[] image = new Byte[filelength]; //建立一个字节数组
  475. fs.Read(image, 0, filelength); //按字节流读取
  476. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  477. fs.Close();
  478. Bitmap bit = new Bitmap(result);
  479. return bit;
  480. }
  481. //图片转成二进制
  482. public static byte[] GetPictureData(string imagepath)
  483. {
  484. /**/
  485. ////根据图片文件的路径使用文件流打开,并保存为byte[]
  486. FileStream FileStream = new FileStream(imagepath, FileMode.Open);
  487. byte[] byData = new byte[FileStream.Length];
  488. FileStream.Read(byData, 0, byData.Length);
  489. FileStream.Close();
  490. return byData;
  491. }
  492. /// <summary>
  493. /// 将image转成bytes
  494. /// </summary>
  495. /// <param name="in_img"></param>
  496. /// <returns></returns>
  497. public static byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  498. {
  499. MemoryStream ms = new MemoryStream();
  500. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  501. return ms.ToArray();
  502. }
  503. /// <summary>
  504. /// Resize图片
  505. /// </summary>
  506. /// <param name="bmp">原始Bitmap</param>
  507. /// <param name="newW">新的宽度</param>
  508. /// <param name="newH">新的高度</param>
  509. /// <returns>处理以后的图片</returns>
  510. public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
  511. {
  512. try
  513. {
  514. Bitmap b = new Bitmap(newW, newH);
  515. Graphics g = Graphics.FromImage(b);
  516.                 // 插值算法的质量
  517.                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  518. g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  519. g.Dispose();
  520. return b;
  521. }
  522. catch
  523. {
  524. return null;
  525. }
  526. }
  527. public static Bitmap GetReZoomBitmap(Bitmap in_bp)
  528. {
  529. Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(in_bp);
  530. Mat dst = new Mat();
  531. Cv2.AdaptiveThreshold(src, dst, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 27, 25);
  532. //绝对缩放,
  533. Mat dst2 = new Mat();
  534. int col = dst.Width;//获取原图像的大小
  535. int rows = dst.Height;
  536. Cv2.Resize(dst, dst2, new OpenCvSharp.Size(4 * col, 4 * rows), 0, 0, InterpolationFlags.Cubic);
  537. return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst2);
  538. }
  539. }
  540. /// <summary>
  541. /// 获取当前桌面显示器相关信息,暂未考虑多显示器
  542. /// </summary>
  543. static public class MyPrimaryScreen
  544. {
  545. #region Win32 API  
  546. [DllImport("user32.dll")]
  547. static extern IntPtr GetDC(IntPtr ptr);
  548. [DllImport("gdi32.dll")]
  549. static extern int GetDeviceCaps(
  550. IntPtr hdc, // handle to DC  
  551. int nIndex // index of capability  
  552. );
  553. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  554. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  555. #endregion
  556. #region DeviceCaps常量  
  557. const int HORZRES = 8;
  558. const int VERTRES = 10;
  559. const int LOGPIXELSX = 88;
  560. const int LOGPIXELSY = 90;
  561. const int DESKTOPVERTRES = 117;
  562. const int DESKTOPHORZRES = 118;
  563. #endregion
  564. #region 属性  
  565. /// <summary>  
  566. /// 获取屏幕分辨率当前物理大小  
  567. /// </summary>  
  568. public static System.Drawing.Size WorkingArea
  569. {
  570. get
  571. {
  572. IntPtr hdc = GetDC(IntPtr.Zero);
  573. System.Drawing.Size size = new System.Drawing.Size();
  574. size.Width = GetDeviceCaps(hdc, HORZRES);
  575. size.Height = GetDeviceCaps(hdc, VERTRES);
  576. ReleaseDC(IntPtr.Zero, hdc);
  577. return size;
  578. }
  579. }
  580. /// <summary>  
  581. /// 当前系统DPI_X 大小 一般为96  
  582. /// </summary>  
  583. public static int DpiX
  584. {
  585. get
  586. {
  587. IntPtr hdc = GetDC(IntPtr.Zero);
  588. int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  589. ReleaseDC(IntPtr.Zero, hdc);
  590. return DpiX;
  591. }
  592. }
  593. /// <summary>  
  594. /// 当前系统DPI_Y 大小 一般为96  
  595. /// </summary>  
  596. public static int DpiY
  597. {
  598. get
  599. {
  600. IntPtr hdc = GetDC(IntPtr.Zero);
  601. int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
  602. ReleaseDC(IntPtr.Zero, hdc);
  603. return DpiX;
  604. }
  605. }
  606. /// <summary>  
  607. /// 获取真实设置的桌面分辨率大小  
  608. /// </summary>  
  609. public static System.Drawing.Size DESKTOP
  610. {
  611. get
  612. {
  613. IntPtr hdc = GetDC(IntPtr.Zero);
  614. System.Drawing.Size size = new System.Drawing.Size();
  615. size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
  616. size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
  617. ReleaseDC(IntPtr.Zero, hdc);
  618. return size;
  619. }
  620. }
  621. /// <summary>  
  622. /// 获取宽度缩放百分比  
  623. /// </summary>  
  624. public static float ScaleX
  625. {
  626. get
  627. {
  628. IntPtr hdc = GetDC(IntPtr.Zero);
  629. int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
  630. int d = GetDeviceCaps(hdc, HORZRES);
  631. float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  632. ReleaseDC(IntPtr.Zero, hdc);
  633. return ScaleX;
  634. }
  635. }
  636. /// <summary>  
  637. /// 获取高度缩放百分比  
  638. /// </summary>  
  639. public static float ScaleY
  640. {
  641. get
  642. {
  643. IntPtr hdc = GetDC(IntPtr.Zero);
  644. float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
  645. ReleaseDC(IntPtr.Zero, hdc);
  646. return ScaleY;
  647. }
  648. }
  649. #endregion
  650. }
  651. }