DrawUtils.Mono.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Windows.Forms;
  4. using FastReport.Dialog;
  5. namespace FastReport.Utils
  6. {
  7. partial class DrawUtils
  8. {
  9. public static Bitmap DrawToBitmap(Control control, bool children)
  10. {
  11. if (control is Form form)
  12. return DrawForm(form);
  13. Bitmap bitmap = new Bitmap(control.Width, control.Height);
  14. using (Graphics gr = Graphics.FromImage(bitmap))
  15. {
  16. gr.Clear(control.BackColor);
  17. }
  18. control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
  19. return bitmap;
  20. }
  21. private static Bitmap DrawForm(Form form)
  22. {
  23. var width = form.FrameSize().Width;
  24. var height = form.FrameSize().Height;
  25. var offset = form.ClientAreaOffset();
  26. Bitmap bitmap = new Bitmap(width, height);
  27. using (Graphics gr = Graphics.FromImage(bitmap))
  28. {
  29. gr.Clear(SystemColors.Window);
  30. using (GraphicsPath gp = new GraphicsPath())
  31. {
  32. var radius = 16;
  33. gp.AddArc(0, 0, radius, radius, 180, 90);
  34. gp.AddLine(radius, 0, width - radius, 0);
  35. gp.AddArc(width - radius - 1, 0, radius, radius, 270, 90);
  36. gp.AddLine(width - 1, radius, width - 1, height - 1);
  37. gp.AddLine(width - 1, height - 1, 0, height - 1);
  38. gp.CloseFigure();
  39. var captionColor = SystemBrushes.InactiveCaption;
  40. gr.FillPath(captionColor, gp);
  41. gr.DrawPath(Pens.Gray, gp);
  42. using (var br = new SolidBrush(form.BackColor))
  43. {
  44. gr.FillRectangle(br, offset.X, offset.Y, form.ClientRectangle.Width, form.ClientRectangle.Height);
  45. }
  46. #if AVALONIA
  47. gr.FontScale = (float)form.DpiScale;
  48. #endif
  49. TextRenderer.DrawText(gr, form.Text, form.Font, new Rectangle(8, 0, width, offset.Y), SystemColors.WindowText, SystemColors.InactiveCaption, TextFormatFlags.VerticalCenter);
  50. }
  51. }
  52. return bitmap;
  53. }
  54. }
  55. }