ImageTools.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PaintDotNet.GeneralAnalysis.Artwork
  10. {
  11. internal class ImageTools
  12. {
  13. public enum OverlapType
  14. {
  15. CENTER = 0, //中间重叠
  16. TOPLEFT = 1, //左上角
  17. }
  18. /// <summary>
  19. /// Bitmap裁剪
  20. /// </summary>
  21. /// <param name="src">原图</param>
  22. /// <param name="cropRect">选择区域</param>
  23. /// <returns></returns>
  24. public static Bitmap Crop(Bitmap src, Rectangle cropRect)
  25. {
  26. Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
  27. using (Graphics g = Graphics.FromImage(target))
  28. {
  29. g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
  30. cropRect,
  31. GraphicsUnit.Pixel);
  32. }
  33. return target;
  34. }
  35. /// <summary>
  36. /// 拼合图片
  37. /// </summary>
  38. /// <param name="bitmaps"></param>
  39. /// <param name="width"></param>
  40. /// <param name="height"></param>
  41. /// <param name="type"></param>
  42. /// <returns></returns>
  43. public static Bitmap Overlap(Bitmap[] bitmaps, int width, int height, OverlapType type)
  44. {
  45. Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  46. float x = 0;
  47. float y = 0;
  48. //load the Bitmap into a Graphics object
  49. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  50. //Set the rendering quality for this Graphics object
  51. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
  52. //haix
  53. for (int i = 0; i < bitmaps.Length; i++)
  54. {
  55. Bitmap img = bitmaps[i];
  56. if (img == null)
  57. {
  58. continue;
  59. }
  60. if (type == OverlapType.CENTER)
  61. {
  62. x = (width - img.Width) / 2.0f;
  63. y = (height - img.Height) / 2.0f;
  64. }
  65. grPhoto.DrawImage(img, new PointF(x, y));
  66. }
  67. return bmPhoto;
  68. }
  69. /// <summary>
  70. /// 生成位置导航窗口图像
  71. /// </summary>
  72. /// <param name="bitmap"></param>
  73. /// <param name="width"></param>
  74. /// <param name="height"></param>
  75. /// <param name="pointF"></param>
  76. /// <returns></returns>
  77. public static Bitmap CreatView(Bitmap bitmap, int width, int height, PointF pointF)
  78. {
  79. Bitmap bmPhoto = new Bitmap(width, height);
  80. for (int y = 0; y < bmPhoto.Height; y++)
  81. {
  82. for (int x = 0; x < bmPhoto.Width; x++)
  83. {
  84. bmPhoto.SetPixel(x, y, Color.FromArgb(0, 0, 0));
  85. }
  86. }
  87. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  88. //Set the rendering quality for this Graphics object
  89. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
  90. grPhoto.DrawImage(bitmap, pointF);
  91. return bmPhoto;
  92. }
  93. /// <summary>
  94. /// 任意角度旋转
  95. /// </summary>
  96. /// <param name="bmp">原始图Bitmap</param>
  97. /// <param name="angle">旋转角度</param>
  98. /// <returns></returns>
  99. public static Bitmap RotateFlip(Bitmap bmp, float angle)
  100. {
  101. int w = bmp.Width + 2;
  102. int h = bmp.Height + 2;
  103. Bitmap tmp = new Bitmap(w, h);
  104. Graphics g = Graphics.FromImage(tmp);
  105. g.DrawImageUnscaled(bmp, 1, 1);
  106. g.Dispose();
  107. GraphicsPath path = new GraphicsPath();
  108. path.AddRectangle(new RectangleF(0f, 0f, w, h));
  109. Matrix mtrx = new Matrix();
  110. mtrx.Rotate(angle);
  111. RectangleF rct = path.GetBounds(mtrx);
  112. Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height);
  113. g = Graphics.FromImage(dst);
  114. g.TranslateTransform(-rct.X, -rct.Y);
  115. g.RotateTransform(angle);
  116. g.InterpolationMode = InterpolationMode.HighQualityBilinear;
  117. g.DrawImageUnscaled(tmp, 0, 0);
  118. g.Dispose();
  119. tmp.Dispose();
  120. return dst;
  121. }
  122. /// <summary>
  123. /// 调整图片的透明度
  124. /// </summary>
  125. /// <param name="src"></param>
  126. /// <param name="num"></param>
  127. /// <returns></returns>
  128. public static Bitmap MakeTransparent(Bitmap src, int num)
  129. {
  130. try
  131. {
  132. int w = src.Width;
  133. int h = src.Height;
  134. Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  135. System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  136. System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  137. unsafe
  138. {
  139. byte* pIn = (byte*)srcData.Scan0.ToPointer();
  140. byte* pOut = (byte*)dstData.Scan0.ToPointer();
  141. byte* p;
  142. int stride = srcData.Stride;
  143. int r, g, b;
  144. for (int y = 0; y < h; y++)
  145. {
  146. for (int x = 0; x < w; x++)
  147. {
  148. p = pIn;
  149. b = pIn[0];
  150. g = pIn[1];
  151. r = pIn[2];
  152. pOut[1] = (byte)g;
  153. pOut[2] = (byte)r;
  154. pOut[3] = (byte)num;
  155. pOut[0] = (byte)b;
  156. pIn += 4;
  157. pOut += 4;
  158. }
  159. pIn += srcData.Stride - w * 4;
  160. pOut += srcData.Stride - w * 4;
  161. }
  162. src.UnlockBits(srcData);
  163. dstBitmap.UnlockBits(dstData);
  164. return dstBitmap;
  165. }
  166. }
  167. catch (Exception)
  168. {
  169. return null;
  170. }
  171. }
  172. /// <summary>
  173. /// 两点间距离
  174. /// </summary>
  175. /// <param name="startPoint"></param>
  176. /// <param name="endPoint"></param>
  177. /// <returns></returns>
  178. public static double CalcDistance(Point startPoint, Point endPoint)
  179. {
  180. int x = System.Math.Abs(endPoint.X - startPoint.X);
  181. int y = System.Math.Abs(endPoint.Y - startPoint.Y);
  182. return Math.Sqrt(x * x + y * y);
  183. }
  184. }
  185. }