DrawRulerHelper.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using SmartCoalApplication.Base.Enum;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace SmartCoalApplication.Base.CommTool
  13. {
  14. /// <summary>
  15. /// 绘制标尺
  16. /// </summary>
  17. public class DrawRulerHelper
  18. {
  19. /// <summary>
  20. /// 缩放图像
  21. /// </summary>
  22. /// <param name="originalImage"></param>
  23. /// <param name="width"></param>
  24. /// <param name="height"></param>
  25. /// <param name="mode"></param>
  26. /// <returns></returns>
  27. public static Image ResizeImage(Image originalImage, int width, int height, ThumbnailMode mode)
  28. {
  29. int towidth = width;
  30. int toheight = height;
  31. int x = 0;
  32. int y = 0;
  33. int initWidth = originalImage.Width;
  34. int initHeight = originalImage.Height;
  35. switch (mode)
  36. {
  37. case ThumbnailMode.UsrHeightWidth: //指定高宽缩放(可能变形)
  38. break;
  39. case ThumbnailMode.UsrHeightWidthBound: //指定高宽缩放(可能变形)(过小则不变)
  40. if (originalImage.Width <= width && originalImage.Height <= height)
  41. {
  42. return originalImage;
  43. }
  44. if (originalImage.Width < width)
  45. {
  46. towidth = originalImage.Width;
  47. }
  48. if (originalImage.Height < height)
  49. {
  50. toheight = originalImage.Height;
  51. }
  52. break;
  53. case ThumbnailMode.UsrWidth: //指定宽,高按比例
  54. toheight = originalImage.Height * width / originalImage.Width;
  55. break;
  56. case ThumbnailMode.UsrWidthBound: //指定宽(过小则不变),高按比例
  57. if (originalImage.Width <= width)
  58. {
  59. return originalImage;
  60. }
  61. else
  62. {
  63. toheight = originalImage.Height * width / originalImage.Width;
  64. }
  65. break;
  66. case ThumbnailMode.UsrHeight: //指定高,宽按比例
  67. towidth = originalImage.Width * height / originalImage.Height;
  68. break;
  69. case ThumbnailMode.UsrHeightBound: //指定高(过小则不变),宽按比例
  70. if (originalImage.Height <= height)
  71. {
  72. return originalImage;
  73. }
  74. else
  75. {
  76. towidth = originalImage.Width * height / originalImage.Height;
  77. }
  78. break;
  79. case ThumbnailMode.Cut: //指定高宽裁减(不变形)
  80. //计算宽高比
  81. double srcScale = (double)originalImage.Width / (double)originalImage.Height;
  82. double destScale = (double)towidth / (double)toheight;
  83. //宽高比相同
  84. if (srcScale - destScale >= 0 && srcScale - destScale <= 0.001)
  85. {
  86. x = 0;
  87. y = 0;
  88. initWidth = originalImage.Width;
  89. initHeight = originalImage.Height;
  90. }
  91. //源宽高比大于目标宽高比
  92. //(源的宽比目标的宽大)
  93. else if (srcScale > destScale)
  94. {
  95. initWidth = originalImage.Height * towidth / toheight;
  96. initHeight = originalImage.Height;
  97. x = (originalImage.Width - initWidth) / 2;
  98. y = 0;
  99. }
  100. //源宽高比小于目标宽高小,源的高度大于目标的高度
  101. else
  102. {
  103. initWidth = originalImage.Width;
  104. initHeight = originalImage.Width * height / towidth;
  105. x = 0;
  106. y = (originalImage.Height - initHeight) / 2;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. Image bitmap = new Bitmap(towidth, toheight);
  113. //新建一个画板
  114. using (Graphics g = Graphics.FromImage(bitmap))
  115. {
  116. //设置高质量插值法
  117. g.CompositingQuality = CompositingQuality.HighQuality;
  118. //设置高质量,低速段呈现的平滑程度
  119. g.SmoothingMode = SmoothingMode.HighQuality;
  120. //在指定的位置上,并按指定大小绘制原图片的指定部分
  121. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, initWidth, initHeight), GraphicsUnit.Pixel);
  122. }
  123. return bitmap;
  124. }
  125. /// <summary>
  126. /// 深拷贝,反射
  127. /// </summary>
  128. /// <param name="obj"></param>
  129. /// <returns></returns>
  130. public static T DeepCopyByReflect<T>(T obj)
  131. {
  132. //如果是字符串或值类型则直接返回
  133. if (obj == null || obj is string || obj.GetType().IsValueType) return obj;
  134. object retval = Activator.CreateInstance(obj.GetType());
  135. FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
  136. foreach (FieldInfo field in fields)
  137. {
  138. try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
  139. catch { }
  140. }
  141. return (T)retval;
  142. }
  143. public static List<T> DeepCopyListByReflect<T>(List<T> objs)
  144. {
  145. List<T> list = new List<T>();
  146. if (objs != null && objs.Count > 0)
  147. {
  148. foreach (T t in objs)
  149. {
  150. list.Add(DeepCopyByReflect(t));
  151. }
  152. }
  153. return list;
  154. }
  155. /// <summary>
  156. /// base64转bitmap
  157. /// </summary>
  158. /// <param name="strbase64"></param>
  159. /// <returns></returns>
  160. public static Bitmap Base64StringToImage(string strbase64)
  161. {
  162. try
  163. {
  164. byte[] arr = Convert.FromBase64String(strbase64);
  165. MemoryStream ms = new MemoryStream(arr);
  166. Bitmap bmp = new Bitmap(ms);
  167. ms.Close();
  168. return bmp;
  169. }
  170. catch (Exception)
  171. {
  172. return null;
  173. }
  174. }
  175. /// <summary>
  176. /// bitmap转base64
  177. /// </summary>
  178. /// <param name="bmp"></param>
  179. /// <returns></returns>
  180. public static string ImgToBase64String(Bitmap source)
  181. {
  182. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(source);
  183. Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  184. string base64 = "";
  185. using (MemoryStream ms = new MemoryStream())
  186. {
  187. bitmap.Save(ms, ImageFormat.Jpeg);
  188. base64 = Convert.ToBase64String(ms.ToArray());
  189. ms.Close();
  190. bitmap.Dispose();
  191. }
  192. return base64;
  193. }
  194. }
  195. }