DrawHelper.cs 7.2 KB

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