DrawFunction.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. private static Dictionary<string, Color> colordic = new Dictionary<string, Color>();
  303. public static Color GetColorBySTDTypeIDForBSEAndSorImage(string in_partcolor, int in_stdtypeid)
  304. {
  305. Color ret_c = new Color();
  306. if (in_stdtypeid < 1000)
  307. {
  308. ret_c = GetColorByEnum(in_stdtypeid);
  309. }
  310. else if (in_stdtypeid >= 1000)
  311. {
  312. //大于等于1000,并且小于10000时,使用用户标准库来分析夹杂物名称
  313. if (!in_partcolor.Contains("#"))
  314. {
  315. in_partcolor = "#" + in_partcolor;
  316. if (colordic.ContainsKey(in_partcolor))//using dictionary to speed up.
  317. {
  318. ret_c = colordic[ in_partcolor];
  319. }
  320. else
  321. {
  322. ret_c = DrawFunction.colorHx16toRGB(in_partcolor);//接收必须是#000000的格式
  323. colordic.Add(in_partcolor, ret_c);
  324. }
  325. }
  326. else
  327. {
  328. if (colordic.ContainsKey(in_partcolor))//using dictionary to speed up.
  329. {
  330. ret_c = colordic[in_partcolor];
  331. }
  332. else
  333. {
  334. ret_c = DrawFunction.colorHx16toRGB(in_partcolor);//接收必须是#000000的格式
  335. colordic.Add(in_partcolor, ret_c);
  336. }
  337. }
  338. }
  339. return ret_c;
  340. }
  341. public static Color GetColorByEnum(int STDID)
  342. {
  343. Color ret_c = new Color();
  344. switch (STDID)
  345. {
  346. case -1:
  347. //INVALID = -1,
  348. //stdName = "无效颗粒";
  349. ret_c = Color.Black;
  350. break;
  351. case 0:
  352. //small = 0;
  353. //stdName = "过小颗粒";
  354. ret_c = Color.Black;
  355. break;
  356. case 1:
  357. //OVERSIZE = 1,
  358. //stdName = "过大颗粒";
  359. ret_c = Color.Black;
  360. break;
  361. case 2:
  362. //AVE_GRAY_NOT_INRANRE = 2,
  363. //stdName = "亮度不在分析范围内的颗粒";
  364. ret_c = Color.Black;
  365. break;
  366. case 3:
  367. //SEARCH_X_RAY = 3,
  368. //stdName = "不进行搜索x-ray分析的颗粒";
  369. ret_c = Color.Black;
  370. break;
  371. case 4:
  372. //LOW_COUNT = 4,
  373. //stdName = "低x-ray计数颗粒";
  374. ret_c = Color.Black;
  375. break;
  376. case 5:
  377. //NO_INTEREST_ELEMENTS = 5,
  378. //stdName = "不含分析元素的颗粒";
  379. ret_c = Color.Black;
  380. break;
  381. case 6:
  382. //ALAYSIS_X_RAY = 6,
  383. //stdName = "不进行x-ray分析的颗粒";
  384. ret_c = Color.Black;
  385. break;
  386. case 7:
  387. //NOT_IDENTIFIED = 7,
  388. //stdName = "未识别颗粒";
  389. ret_c = Color.Black;
  390. break;
  391. case 8:
  392. //NOT_IDENTIFIED = 8,
  393. //stdName = "未识别颗粒";
  394. ret_c = Color.Black;
  395. break;
  396. case 9:
  397. //NOT_IDENTIFIED = 9,
  398. //stdName = "未识别颗粒";
  399. ret_c = Color.Black;
  400. break;
  401. default:
  402. ret_c = Color.Black;
  403. break;
  404. }
  405. return ret_c;
  406. }
  407. /// <summary>
  408. /// 获取一个自绘模仿进度条的bitemap对象,传入进度的值,0-100之间
  409. /// </summary>
  410. /// <param name="process_value"></param>
  411. /// <returns></returns>
  412. public static Bitmap GetProcessBitmap(int process_value)
  413. {
  414. Bitmap bmp = new Bitmap(104, 30); //这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
  415. Graphics g = Graphics.FromImage(bmp);
  416. g.Clear(Color.White); //背景填白色
  417. //g.FillRectangle(Brushes.Red, 2, 2, this.Press, 26); //普通效果
  418. //填充渐变效果
  419. SolidBrush drawBrush = new SolidBrush(Color.Black);
  420. 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);
  421. g.DrawString(process_value.ToString(), new Font("宋体", 9), drawBrush, 50, 8);
  422. return bmp;
  423. }
  424. /// <summary>
  425. /// 获取一个自绘模仿进度条的bitemap对象,传入进度的值,0-100之间[重载可以自定设置背景色]
  426. /// </summary>
  427. /// <param name="process_value"></param>
  428. /// <param name="back_color"></param>
  429. /// <returns></returns>
  430. public static Bitmap GetProcessBitmap(float process_value, Color back_color)
  431. {
  432. if (process_value < 0)
  433. process_value = 0;
  434. if (process_value > 100)
  435. process_value = 100;
  436. Bitmap bmp = new Bitmap(104, 30); //这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
  437. Graphics g = Graphics.FromImage(bmp);
  438. g.Clear(back_color); //背景填白色
  439. //g.FillRectangle(Brushes.Red, 2, 2, this.Press, 26); //普通效果
  440. //填充渐变效果
  441. SolidBrush drawBrush = new SolidBrush(Color.Black);
  442. 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);
  443. g.DrawString(process_value.ToString(), new Font("宋体", 9), drawBrush, 50, 8);
  444. return bmp;
  445. }
  446. /// <summary>
  447. /// [颜色:16进制转成RGB]
  448. /// </summary>
  449. /// <param name="strColor">设置16进制颜色 [返回RGB]</param>
  450. /// <returns></returns>
  451. public static System.Drawing.Color colorHx16toRGB(string strHxColor)
  452. {
  453. try
  454. {
  455. if (strHxColor.Length == 0)
  456. {//如果为空
  457. return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
  458. }
  459. else
  460. {//转换颜色
  461. if (strHxColor.IndexOf('#') > -1)
  462. {
  463. //如果颜色格式是 #0000FF 格式的
  464. 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));
  465. }
  466. else
  467. {
  468. //如果颜色格式是 0000FF 格式的
  469. 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));
  470. }
  471. }
  472. }
  473. catch
  474. {//设为黑色
  475. return System.Drawing.Color.FromArgb(0, 0, 0);
  476. }
  477. }
  478. /// <summary>
  479. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  480. /// </summary>
  481. /// <param name="path"></param>
  482. /// <returns></returns>
  483. public static Bitmap ReadImageFile(string path)
  484. {
  485. if (!File.Exists(path))
  486. {
  487. return null;//文件不存在
  488. }
  489. //FileStream fs = File.OpenRead(path); //OpenRead
  490. //int filelength = 0;
  491. //filelength = (int)fs.Length; //获得文件长度
  492. //Byte[] image = new Byte[filelength]; //建立一个字节数组
  493. //fs.Read(image, 0, filelength); //按字节流读取
  494. //System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  495. //fs.Close();
  496. //Bitmap bit = new Bitmap(result);
  497. //return bit;
  498. var bit = Bitmap.FromFile(path);
  499. return (Bitmap)bit;
  500. }
  501. //图片转成二进制
  502. public static byte[] GetPictureData(string imagepath)
  503. {
  504. /**/
  505. ////根据图片文件的路径使用文件流打开,并保存为byte[]
  506. FileStream FileStream = new FileStream(imagepath, FileMode.Open);
  507. byte[] byData = new byte[FileStream.Length];
  508. FileStream.Read(byData, 0, byData.Length);
  509. FileStream.Close();
  510. return byData;
  511. }
  512. /// <summary>
  513. /// 将image转成bytes
  514. /// </summary>
  515. /// <param name="in_img"></param>
  516. /// <returns></returns>
  517. public static byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  518. {
  519. MemoryStream ms = new MemoryStream();
  520. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  521. return ms.ToArray();
  522. }
  523. /// <summary>
  524. /// Resize图片
  525. /// </summary>
  526. /// <param name="bmp">原始Bitmap</param>
  527. /// <param name="newW">新的宽度</param>
  528. /// <param name="newH">新的高度</param>
  529. /// <returns>处理以后的图片</returns>
  530. public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
  531. {
  532. try
  533. {
  534. Bitmap b = new Bitmap(newW, newH);
  535. Graphics g = Graphics.FromImage(b);
  536.                 // 插值算法的质量
  537.                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  538. g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  539. g.Dispose();
  540. return b;
  541. }
  542. catch
  543. {
  544. return null;
  545. }
  546. }
  547. public static Bitmap GetReZoomBitmap(Bitmap in_bp)
  548. {
  549. Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(in_bp);
  550. Mat dst = new Mat();
  551. Cv2.AdaptiveThreshold(src, dst, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 27, 25);
  552. //绝对缩放,
  553. Mat dst2 = new Mat();
  554. int col = dst.Width;//获取原图像的大小
  555. int rows = dst.Height;
  556. Cv2.Resize(dst, dst2, new OpenCvSharp.Size(4 * col, 4 * rows), 0, 0, InterpolationFlags.Cubic);
  557. return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst2);
  558. }
  559. }
  560. /// <summary>
  561. /// 获取当前桌面显示器相关信息,暂未考虑多显示器
  562. /// </summary>
  563. static public class MyPrimaryScreen
  564. {
  565. #region Win32 API  
  566. [DllImport("user32.dll")]
  567. static extern IntPtr GetDC(IntPtr ptr);
  568. [DllImport("gdi32.dll")]
  569. static extern int GetDeviceCaps(
  570. IntPtr hdc, // handle to DC  
  571. int nIndex // index of capability  
  572. );
  573. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  574. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  575. #endregion
  576. #region DeviceCaps常量  
  577. const int HORZRES = 8;
  578. const int VERTRES = 10;
  579. const int LOGPIXELSX = 88;
  580. const int LOGPIXELSY = 90;
  581. const int DESKTOPVERTRES = 117;
  582. const int DESKTOPHORZRES = 118;
  583. #endregion
  584. #region 属性  
  585. /// <summary>  
  586. /// 获取屏幕分辨率当前物理大小  
  587. /// </summary>  
  588. public static System.Drawing.Size WorkingArea
  589. {
  590. get
  591. {
  592. IntPtr hdc = GetDC(IntPtr.Zero);
  593. System.Drawing.Size size = new System.Drawing.Size();
  594. size.Width = GetDeviceCaps(hdc, HORZRES);
  595. size.Height = GetDeviceCaps(hdc, VERTRES);
  596. ReleaseDC(IntPtr.Zero, hdc);
  597. return size;
  598. }
  599. }
  600. /// <summary>  
  601. /// 当前系统DPI_X 大小 一般为96  
  602. /// </summary>  
  603. public static int DpiX
  604. {
  605. get
  606. {
  607. IntPtr hdc = GetDC(IntPtr.Zero);
  608. int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  609. ReleaseDC(IntPtr.Zero, hdc);
  610. return DpiX;
  611. }
  612. }
  613. /// <summary>  
  614. /// 当前系统DPI_Y 大小 一般为96  
  615. /// </summary>  
  616. public static int DpiY
  617. {
  618. get
  619. {
  620. IntPtr hdc = GetDC(IntPtr.Zero);
  621. int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
  622. ReleaseDC(IntPtr.Zero, hdc);
  623. return DpiX;
  624. }
  625. }
  626. /// <summary>  
  627. /// 获取真实设置的桌面分辨率大小  
  628. /// </summary>  
  629. public static System.Drawing.Size DESKTOP
  630. {
  631. get
  632. {
  633. IntPtr hdc = GetDC(IntPtr.Zero);
  634. System.Drawing.Size size = new System.Drawing.Size();
  635. size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
  636. size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
  637. ReleaseDC(IntPtr.Zero, hdc);
  638. return size;
  639. }
  640. }
  641. /// <summary>  
  642. /// 获取宽度缩放百分比  
  643. /// </summary>  
  644. public static float ScaleX
  645. {
  646. get
  647. {
  648. IntPtr hdc = GetDC(IntPtr.Zero);
  649. int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
  650. int d = GetDeviceCaps(hdc, HORZRES);
  651. float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  652. ReleaseDC(IntPtr.Zero, hdc);
  653. return ScaleX;
  654. }
  655. }
  656. /// <summary>  
  657. /// 获取高度缩放百分比  
  658. /// </summary>  
  659. public static float ScaleY
  660. {
  661. get
  662. {
  663. IntPtr hdc = GetDC(IntPtr.Zero);
  664. float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
  665. ReleaseDC(IntPtr.Zero, hdc);
  666. return ScaleY;
  667. }
  668. }
  669. #endregion
  670. }
  671. }