GdiPlusExt.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Text;
  5. namespace BsWidget
  6. {
  7. internal static class GdiPlusExt
  8. {
  9. public static void ClearRect(this Graphics g, Color color, float x, float y, float w, float h)
  10. {
  11. g.SetClip(new RectangleF(x, y, w, h));
  12. g.Clear(color);
  13. g.ResetClip();
  14. }
  15. public static void SetHighQuality(this Graphics g)
  16. {
  17. g.CompositingMode = CompositingMode.SourceOver;
  18. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  19. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  20. g.SmoothingMode = SmoothingMode.HighQuality;
  21. g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  22. }
  23. public static SizeF DrawStringWithOutline(this Graphics g, string text, Font font, float x, float y, Color outlineColor, Color fillColor, float outlineWidth = 1)
  24. {
  25. // assuming g is the Graphics object on which you want to draw the text
  26. using var p = new GraphicsPath();
  27. p.AddString(
  28. text, // text to draw
  29. font.FontFamily, // or any other font family
  30. (int)FontStyle.Regular, // font style (bold, italic, etc.)
  31. g.DpiY * font.Size / 72, // em size
  32. new PointF(x, y), // location where to draw text
  33. new StringFormat()); // set options here (e.g. center alignment)
  34. using var outlinePen = new Pen(outlineColor, outlineWidth);
  35. using var fillBrush = new SolidBrush(fillColor);
  36. g.FillPath(fillBrush, p);
  37. g.DrawPath(outlinePen, p);
  38. return p.GetBounds().Size;
  39. }
  40. public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, RectangleF bounds, int cornerRadius)
  41. {
  42. if (graphics == null)
  43. throw new ArgumentNullException("graphics");
  44. if (pen == null)
  45. throw new ArgumentNullException("pen");
  46. using var path = RoundedRect(bounds, cornerRadius);
  47. graphics.DrawPath(pen, path);
  48. }
  49. public static void FillRoundedRectangle(this Graphics graphics, Brush brush, RectangleF bounds, float cornerRadius)
  50. {
  51. if (graphics == null)
  52. throw new ArgumentNullException("graphics");
  53. if (brush == null)
  54. throw new ArgumentNullException("brush");
  55. using var path = RoundedRect(bounds, cornerRadius);
  56. graphics.FillPath(brush, path);
  57. }
  58. public static SizeF DrawStringWithRoundedRect(this Graphics g, string text, Font font, float x, float y, Brush textbBrushe, Brush bgBrush, float radus = 10)
  59. {
  60. var sz = g.MeasureString(text, font);
  61. g.FillRoundedRectangle(bgBrush, new RectangleF(x, y, sz.Width, sz.Height), radus);
  62. g.DrawString(text, font, textbBrushe, x, y);
  63. return sz;
  64. }
  65. private static GraphicsPath RoundedRect(RectangleF bounds, float radius)
  66. {
  67. var diameter = radius * 2;
  68. var size = new SizeF(diameter, diameter);
  69. var arc = new RectangleF(bounds.Location, size);
  70. var path = new GraphicsPath();
  71. if (Math.Abs(radius) < 0.00001)
  72. {
  73. path.AddRectangle(bounds);
  74. return path;
  75. }
  76. // top left arc
  77. path.AddArc(arc, 180, 90);
  78. // top right arc
  79. arc.X = bounds.Right - diameter;
  80. path.AddArc(arc, 270, 90);
  81. // bottom right arc
  82. arc.Y = bounds.Bottom - diameter;
  83. path.AddArc(arc, 0, 90);
  84. // bottom left arc
  85. arc.X = bounds.Left;
  86. path.AddArc(arc, 90, 90);
  87. path.CloseFigure();
  88. return path;
  89. }
  90. }
  91. }