DrawHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DrawHelper.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System.Drawing;
  11. using System.Drawing.Drawing2D;
  12. using System.Windows.Forms;
  13. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  14. {
  15. internal static class DrawHelper
  16. {
  17. public static Point RtlTransform(Control control, Point point)
  18. {
  19. if (control.RightToLeft != RightToLeft.Yes)
  20. return point;
  21. else
  22. return new Point(control.Right - point.X, point.Y);
  23. }
  24. public static Rectangle RtlTransform(Control control, Rectangle rectangle)
  25. {
  26. if (control.RightToLeft != RightToLeft.Yes)
  27. return rectangle;
  28. else
  29. return new Rectangle(control.ClientRectangle.Right - rectangle.Right, rectangle.Y, rectangle.Width, rectangle.Height);
  30. }
  31. public static GraphicsPath GetRoundedCornerTab(GraphicsPath graphicsPath, Rectangle rect, bool upCorner)
  32. {
  33. if (graphicsPath == null)
  34. graphicsPath = new GraphicsPath();
  35. else
  36. graphicsPath.Reset();
  37. int curveSize = 6;
  38. if (upCorner)
  39. {
  40. graphicsPath.AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top + curveSize / 2);
  41. graphicsPath.AddArc(new Rectangle(rect.Left, rect.Top, curveSize, curveSize), 180, 90);
  42. graphicsPath.AddLine(rect.Left + curveSize / 2, rect.Top, rect.Right - curveSize / 2, rect.Top);
  43. graphicsPath.AddArc(new Rectangle(rect.Right - curveSize, rect.Top, curveSize, curveSize), -90, 90);
  44. graphicsPath.AddLine(rect.Right, rect.Top + curveSize / 2, rect.Right, rect.Bottom);
  45. }
  46. else
  47. {
  48. graphicsPath.AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom - curveSize / 2);
  49. graphicsPath.AddArc(new Rectangle(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize), 0, 90);
  50. graphicsPath.AddLine(rect.Right - curveSize / 2, rect.Bottom, rect.Left + curveSize / 2, rect.Bottom);
  51. graphicsPath.AddArc(new Rectangle(rect.Left, rect.Bottom - curveSize, curveSize, curveSize), 90, 90);
  52. graphicsPath.AddLine(rect.Left, rect.Bottom - curveSize / 2, rect.Left, rect.Top);
  53. }
  54. return graphicsPath;
  55. }
  56. public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap)
  57. {
  58. return CalculateGraphicsPathFromBitmap(bitmap, Color.Empty);
  59. }
  60. // From http://edu.cnzz.cn/show_3281.html
  61. public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap, Color colorTransparent)
  62. {
  63. GraphicsPath graphicsPath = new GraphicsPath();
  64. if (colorTransparent == Color.Empty)
  65. colorTransparent = bitmap.GetPixel(0, 0);
  66. for (int row = 0; row < bitmap.Height; row++)
  67. {
  68. int colOpaquePixel = 0;
  69. for (int col = 0; col < bitmap.Width; col++)
  70. {
  71. if (bitmap.GetPixel(col, row) != colorTransparent)
  72. {
  73. colOpaquePixel = col;
  74. int colNext = col;
  75. for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
  76. if (bitmap.GetPixel(colNext, row) == colorTransparent)
  77. break;
  78. graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
  79. col = colNext;
  80. }
  81. }
  82. }
  83. return graphicsPath;
  84. }
  85. }
  86. }