DrawHelper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. namespace UAS_MES_NEW.PublicMethod
  4. {
  5. class DrawHelper
  6. {
  7. #region RendererBackground 渲染背景图片,使背景图片不失真
  8. /// <summary>
  9. /// 渲染背景图片,使背景图片不失真
  10. /// </summary>
  11. /// <param name="g"></param>
  12. /// <param name="rect"></param>
  13. /// <param name="backgroundImage"></param>
  14. /// <param name="method"></param>
  15. public static void RendererBackground(Graphics g, Rectangle rect, Image backgroundImage, bool method)
  16. {
  17. if (!method)
  18. {
  19. g.DrawImage(backgroundImage, new Rectangle(rect.X + 0, rect.Y, 5, rect.Height), 0, 0, 5, backgroundImage.Height, GraphicsUnit.Pixel);
  20. g.DrawImage(backgroundImage, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, rect.Height), 5, 0, backgroundImage.Width - 10, backgroundImage.Height, GraphicsUnit.Pixel);
  21. g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - 5, rect.Y, 5, rect.Height), backgroundImage.Width - 5, 0, 5, backgroundImage.Height, GraphicsUnit.Pixel);
  22. }
  23. else
  24. {
  25. DrawHelper.RendererBackground(g, rect, 5, backgroundImage);
  26. }
  27. }
  28. /// <summary>
  29. /// 渲染背景图片,使背景图片不失真
  30. /// </summary>
  31. /// <param name="g"></param>
  32. /// <param name="rect"></param>
  33. /// <param name="cut"></param>
  34. /// <param name="backgroundImage"></param>
  35. public static void RendererBackground(Graphics g, Rectangle rect, int cut, Image backgroundImage)
  36. {
  37. //左上角
  38. g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y, cut, cut), 0, 0, cut, cut, GraphicsUnit.Pixel);
  39. //上边
  40. g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y, rect.Width - cut * 2, cut), cut, 0, backgroundImage.Width - cut * 2, cut, GraphicsUnit.Pixel);
  41. //右上角
  42. g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y, cut, cut), backgroundImage.Width - cut, 0, cut, cut, GraphicsUnit.Pixel);
  43. //左边
  44. g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y + cut, cut, rect.Height - cut * 2), 0, cut, cut, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
  45. //左下角
  46. g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y + rect.Height - cut, cut, cut), 0, backgroundImage.Height - cut, cut, cut, GraphicsUnit.Pixel);
  47. //右边
  48. g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y + cut, cut, rect.Height - cut * 2), backgroundImage.Width - cut, cut, cut, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
  49. //右下角
  50. g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y + rect.Height - cut, cut, cut), backgroundImage.Width - cut, backgroundImage.Height - cut, cut, cut, GraphicsUnit.Pixel);
  51. //下边
  52. g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y + rect.Height - cut, rect.Width - cut * 2, cut), cut, backgroundImage.Height - cut, backgroundImage.Width - cut * 2, cut, GraphicsUnit.Pixel);
  53. //平铺中间
  54. g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y + cut, rect.Width - cut * 2, rect.Height - cut * 2), cut, cut, backgroundImage.Width - cut * 2, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
  55. }
  56. #endregion
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="g"></param>
  61. /// <param name="image"></param>
  62. /// <param name="x1"></param>
  63. /// <param name="y1"></param>
  64. /// <param name="width1"></param>
  65. /// <param name="height1"></param>
  66. /// <param name="x2"></param>
  67. /// <param name="y2"></param>
  68. /// <param name="width2"></param>
  69. /// <param name="height2"></param>
  70. public static void DrawImage(Graphics g, Image image, int x1, int y1, int width1, int height1, int x2, int y2, int width2, int height2)
  71. {
  72. g.DrawImage(image, new Rectangle(x1, y1, width1, height1), x2, y2, width2, height2, GraphicsUnit.Pixel);
  73. }
  74. #region CreateRoundPath 构建圆角路径
  75. /// <summary>
  76. /// 构建圆角路径
  77. /// </summary>
  78. /// <param name="rect"></param>
  79. /// <param name="cornerRadius"></param>
  80. /// <returns></returns>
  81. public static GraphicsPath CreateRoundPath(Rectangle rect, int cornerRadius)
  82. {
  83. GraphicsPath roundedRect = new GraphicsPath();
  84. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  85. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  86. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  87. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  88. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  89. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  90. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  91. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  92. roundedRect.CloseFigure();
  93. return roundedRect;
  94. }
  95. /// <summary>
  96. /// 构建圆角路径
  97. /// </summary>
  98. /// <param name="r"></param>
  99. /// <param name="r1"></param>
  100. /// <param name="r2"></param>
  101. /// <param name="r3"></param>
  102. /// <param name="r4"></param>
  103. /// <returns></returns>
  104. public static GraphicsPath CreateRoundRect(RectangleF r, float r1, float r2, float r3, float r4)
  105. {
  106. float x = r.X;
  107. float y = r.Y;
  108. float width = r.Width;
  109. float height = r.Height;
  110. GraphicsPath path = new GraphicsPath();
  111. path.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y);
  112. path.AddLine(x + r1, y, (x + width) - r2, y);
  113. path.AddBezier((x + width) - r2, y, x + width, y, x + width, y + r2, x + width, y + r2);
  114. path.AddLine((float)(x + width), (float)(y + r2), (float)(x + width), (float)((y + height) - r3));
  115. path.AddBezier((float)(x + width), (float)((y + height) - r3), (float)(x + width), (float)(y + height), (float)((x + width) - r3), (float)(y + height), (float)((x + width) - r3), (float)(y + height));
  116. path.AddLine((float)((x + width) - r3), (float)(y + height), (float)(x + r4), (float)(y + height));
  117. path.AddBezier(x + r4, y + height, x, y + height, x, (y + height) - r4, x, (y + height) - r4);
  118. path.AddLine(x, (y + height) - r4, x, y + r1);
  119. return path;
  120. }
  121. #endregion
  122. }
  123. }