Gradients.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Imaging;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11.  
  12. namespace DsImagingWeb.Demos
  13. {
  14. // Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
  15. public class Gradients
  16. {
  17. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  18. {
  19. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  20. var Inch = dpi;
  21. using (var g = bmp.CreateGraphics(Color.RoyalBlue))
  22. {
  23. var testRectSize = new SizeF(Inch * 6, Inch);
  24. var dy = Inch / 6;
  25. // TextLayout to draw labels:
  26. var tl = g.CreateTextLayout();
  27. tl.DefaultFormat.FontSize = Inch / 6;
  28. tl.DefaultFormat.ForeColor = Color.Chartreuse;
  29. tl.MaxWidth = testRectSize.Width;
  30. tl.MaxHeight = testRectSize.Height;
  31. tl.TextAlignment = TextAlignment.Center;
  32. tl.ParagraphAlignment = ParagraphAlignment.Center;
  33. // Note 1:
  34. var rc = Common.Util.AddNote("Linear gradients using LinearGradientBrush:", g, new RectangleF(Inch, Inch / 2, 500, 100));
  35. // Text insertion point:
  36. PointF ip = new PointF(rc.Left, rc.Bottom + dy);
  37. // Local action to draw a gradient-filled rectangle:
  38. Action<GrapeCity.Documents.Drawing.Brush, string> drawSwatch = (b_, txt_) =>
  39. {
  40. var rect = new RectangleF(ip, testRectSize);
  41. // Fill the rectangle with a gradient brush:
  42. g.FillRectangle(rect, b_);
  43. // Draw a border, text info etc:
  44. g.DrawRectangle(rect, Color.Magenta);
  45. tl.Clear();
  46. tl.Append(txt_);
  47. tl.MaxHeight = testRectSize.Height;
  48. tl.MaxWidth = testRectSize.Width;
  49. tl.PerformLayout(true);
  50. g.DrawTextLayout(tl, ip);
  51. ip.Y += rect.Height + dy;
  52. };
  53. // LinearGradientBrush:
  54. // Horizontal gradient:
  55. LinearGradientBrush linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
  56. drawSwatch(linearGradBrush, $"Linear gradient\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  57. // Vertical gradient:
  58. linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Green, new PointF(0, 1));
  59. drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  60. // Diagonal gradient (increase swatch height to better show diagonal):
  61. testRectSize.Height *= 2;
  62. linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Teal, new PointF(1, 1));
  63. drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  64. // RadialGradientBrush
  65. rc = Common.Util.AddNote("Radial gradients using RadialGradientBrush:", g, new RectangleF(ip, new SizeF(500, 100)));
  66. ip.Y = rc.Bottom + dy;
  67. // Centered:
  68. // testRectSize.Height *= 2;
  69. RadialGradientBrush radialGradBrush = new RadialGradientBrush(Color.Orange, Color.Purple);
  70. drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
  71. // Center in bottom right corner:
  72. radialGradBrush = new RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, new PointF(1, 1));
  73. drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
  74. }
  75. // Done:
  76. return bmp;
  77. }
  78. }
  79. }
  80.