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