ImageDrawRect.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. namespace LYFZ.ComponentLibrary
  6. {
  7. public class ImageDrawRect
  8. {
  9. /// <summary>
  10. /// 枚举按钮状态
  11. /// </summary>
  12. public enum State
  13. {
  14. Normal = 1,//按钮默认时
  15. MouseOver = 2,//鼠标移上按钮时
  16. MouseDown = 3,//鼠标按下按钮时
  17. Disable = 4,//当不启用按钮时(也就是按钮属性Enabled==Ture时)
  18. Default = 5//控件得到Tab焦点时
  19. }
  20. /// <summary>
  21. /// 绘图对像
  22. /// </summary>
  23. /// <param name="g">绘图对像</param>
  24. /// <param name="img">图片</param>
  25. /// <param name="r">绘置的图片大小、坐标</param>
  26. /// <param name="lr">绘置的图片边界</param>
  27. /// <param name="index">当前状态</param>
  28. /// <param name="Totalindex">状态总数</param>
  29. public static void DrawRect(Graphics g, Bitmap img, Rectangle r, Rectangle lr, int index, int Totalindex)
  30. {
  31. if (img == null) return;
  32. Rectangle r1, r2;
  33. int x = (index - 1) * img.Width / Totalindex;
  34. int y = 0;
  35. int x1 = r.Left;
  36. int y1 = r.Top;
  37. if (r.Height > img.Height && r.Width <= img.Width / Totalindex)
  38. {
  39. r1 = new Rectangle(x, y, img.Width / Totalindex, lr.Top);
  40. r2 = new Rectangle(x1, y1, r.Width, lr.Top);
  41. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  42. r1 = new Rectangle(x, y + lr.Top, img.Width / Totalindex, img.Height - lr.Top - lr.Bottom);
  43. r2 = new Rectangle(x1, y1 + lr.Top, r.Width, r.Height - lr.Top - lr.Bottom);
  44. if ((lr.Top + lr.Bottom) == 0) r1.Height = r1.Height - 1;
  45. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  46. r1 = new Rectangle(x, y + img.Height - lr.Bottom, img.Width / Totalindex, lr.Bottom);
  47. r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, r.Width, lr.Bottom);
  48. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  49. }
  50. else
  51. if (r.Height <= img.Height && r.Width > img.Width / Totalindex)
  52. {
  53. r1 = new Rectangle(x, y, lr.Left, img.Height);
  54. r2 = new Rectangle(x1, y1, lr.Left, r.Height);
  55. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  56. r1 = new Rectangle(x + lr.Left, y, img.Width / Totalindex - lr.Left - lr.Right, img.Height);
  57. r2 = new Rectangle(x1 + lr.Left, y1, r.Width - lr.Left - lr.Right, r.Height);
  58. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  59. r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, img.Height);
  60. r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, r.Height);
  61. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  62. }
  63. else
  64. if (r.Height <= img.Height && r.Width <= img.Width / Totalindex) { r1 = new Rectangle((index - 1) * img.Width / Totalindex, 0, img.Width / Totalindex, img.Height); g.DrawImage(img, new Rectangle(x1, y1, r.Width, r.Height), r1, GraphicsUnit.Pixel); }
  65. else if (r.Height > img.Height && r.Width > img.Width / Totalindex)
  66. {
  67. //top-left
  68. r1 = new Rectangle(x, y, lr.Left, lr.Top);
  69. r2 = new Rectangle(x1, y1, lr.Left, lr.Top);
  70. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  71. //top-bottom
  72. r1 = new Rectangle(x, y + img.Height - lr.Bottom, lr.Left, lr.Bottom);
  73. r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, lr.Left, lr.Bottom);
  74. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  75. //left
  76. r1 = new Rectangle(x, y + lr.Top, lr.Left, img.Height - lr.Top - lr.Bottom);
  77. r2 = new Rectangle(x1, y1 + lr.Top, lr.Left, r.Height - lr.Top - lr.Bottom);
  78. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  79. //top
  80. r1 = new Rectangle(x + lr.Left, y,
  81. img.Width / Totalindex - lr.Left - lr.Right, lr.Top);
  82. r2 = new Rectangle(x1 + lr.Left, y1,
  83. r.Width - lr.Left - lr.Right, lr.Top);
  84. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  85. //right-top
  86. r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, lr.Top);
  87. r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, lr.Top);
  88. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  89. //Right
  90. r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + lr.Top,
  91. lr.Right, img.Height - lr.Top - lr.Bottom);
  92. r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + lr.Top,
  93. lr.Right, r.Height - lr.Top - lr.Bottom);
  94. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  95. //right-bottom
  96. r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + img.Height - lr.Bottom,
  97. lr.Right, lr.Bottom);
  98. r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + r.Height - lr.Bottom,
  99. lr.Right, lr.Bottom);
  100. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  101. //bottom
  102. r1 = new Rectangle(x + lr.Left, y + img.Height - lr.Bottom,
  103. img.Width / Totalindex - lr.Left - lr.Right, lr.Bottom);
  104. r2 = new Rectangle(x1 + lr.Left, y1 + r.Height - lr.Bottom,
  105. r.Width - lr.Left - lr.Right, lr.Bottom);
  106. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  107. //Center
  108. r1 = new Rectangle(x + lr.Left, y + lr.Top,
  109. img.Width / Totalindex - lr.Left - lr.Right, img.Height - lr.Top - lr.Bottom);
  110. r2 = new Rectangle(x1 + lr.Left, y1 + lr.Top,
  111. r.Width - lr.Left - lr.Right, r.Height - lr.Top - lr.Bottom);
  112. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  113. }
  114. }
  115. /// <summary>
  116. /// 绘图对像
  117. /// </summary>
  118. /// <param name="g"> 绘图对像</param>
  119. /// <param name="obj">图片对像</param>
  120. /// <param name="r">绘置的图片大小、坐标</param>
  121. /// <param name="index">当前状态</param>
  122. /// <param name="Totalindex">状态总数</param>
  123. public static void DrawRect(Graphics g, Bitmap img, Rectangle r, int index, int Totalindex)
  124. {
  125. if (img == null) return;
  126. int width = img.Width / Totalindex;
  127. int height = img.Height;
  128. Rectangle r1, r2;
  129. int x = (index - 1) * width;
  130. int y = 0;
  131. r1 = new Rectangle(x, y, width, height);
  132. r2 = new Rectangle(r.Left, r.Top, r.Width, r.Height);
  133. g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
  134. }
  135. /// <summary>
  136. /// 设置图片的位置定位
  137. /// </summary>
  138. /// <param name="imgs">要绘制图片对象的尺寸</param>
  139. /// <param name="imgr">要进行绘制的控件 Rectangle 对象</param>
  140. /// <param name="imageAlign">定位枚举</param>
  141. /// <returns></returns>
  142. public static Rectangle SetLocation(Size imgs, Rectangle imgr, ContentAlignment imageAlign)
  143. {
  144. imgr = HAlignWithin(imgs, imgr, imageAlign);
  145. imgr = VAlignWithin(imgs, imgr, imageAlign);
  146. return imgr;
  147. }
  148. /// <summary>
  149. /// 水平横向对齐
  150. /// </summary>
  151. /// <param name="imgs">要绘制图片对象的尺寸</param>
  152. /// <param name="imgr">要进行绘制的控件 Rectangle 对象</param>
  153. /// <param name="imageAlign">定位枚举</param>
  154. /// <returns></returns>
  155. public static Rectangle HAlignWithin(Size imgs, Rectangle imgr, ContentAlignment imageAlign)
  156. {
  157. switch (imageAlign)
  158. {
  159. case ContentAlignment.BottomCenter:
  160. case ContentAlignment.TopCenter:
  161. case ContentAlignment.MiddleCenter:
  162. imgr.X = (imgr.Width - imgs.Width) / 2;
  163. break;
  164. case ContentAlignment.BottomRight:
  165. case ContentAlignment.TopRight:
  166. case ContentAlignment.MiddleRight:
  167. imgr.X = (imgr.Width - imgs.Width);
  168. break;
  169. case ContentAlignment.MiddleLeft:
  170. case ContentAlignment.TopLeft:
  171. case ContentAlignment.BottomLeft:
  172. imgr.X = 0;
  173. break;
  174. }
  175. return imgr;
  176. }
  177. /// <summary>
  178. /// 垂直竖向对齐
  179. /// </summary>
  180. /// <param name="imgs">要绘制图片对象的尺寸</param>
  181. /// <param name="imgr">要进行绘制的控件 Rectangle 对象</param>
  182. /// <param name="imageAlign">定位枚举</param>
  183. /// <returns></returns>
  184. public static Rectangle VAlignWithin(Size imgs, Rectangle imgr, ContentAlignment imageAlign)
  185. {
  186. switch (imageAlign)
  187. {
  188. case ContentAlignment.BottomCenter:
  189. case ContentAlignment.BottomLeft:
  190. case ContentAlignment.BottomRight:
  191. imgr.Y = imgr.Height - imgs.Height;
  192. break;
  193. case ContentAlignment.MiddleCenter:
  194. case ContentAlignment.MiddleLeft:
  195. case ContentAlignment.MiddleRight:
  196. imgr.Y = (imgr.Height - imgs.Height) / 2;
  197. break;
  198. case ContentAlignment.TopCenter:
  199. case ContentAlignment.TopLeft:
  200. case ContentAlignment.TopRight:
  201. imgr.Y = 0;
  202. break;
  203. }
  204. return imgr;
  205. }
  206. }
  207. }