Antialiasing.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 System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This sample shows the different (anti)aliasing modes of rendering text:
  19. // - No anti-aliasing, very fast but poor quality (not recommended).
  20. // - Fast anti-aliasing. This is the default that provides good quality and fast rendering.
  21. // - Slow anti-aliasing. Highest quality but slower than the default.
  22. public class Antialiasing
  23. {
  24. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  25. {
  26. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  27. using var g = bmp.CreateGraphics(Color.FromArgb(unchecked((int)0xffccf2ff)));
  28. g.Renderer.Multithreaded = true;
  29.  
  30. var text = Common.Util.LoremIpsum();
  31. var tsize = new SizeF(bmp.Width / 2, bmp.Height / 2);
  32. var pad = 96f / 4;
  33.  
  34. var tfcap = new TextFormat()
  35. {
  36. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Bold.ttf")),
  37. FontSize = 16,
  38. };
  39.  
  40. var tl = g.CreateTextLayout();
  41. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"));
  42. tl.DefaultFormat.FontSize = 14;
  43. tl.TextAlignment = TextAlignment.Justified;
  44. tl.FirstLineIndent = 96 / 2;
  45. tl.ParagraphSpacing = 96 / 8;
  46. tl.MaxWidth = tsize.Width;
  47. tl.MaxHeight = tsize.Height;
  48. tl.MarginAll = pad;
  49.  
  50. var tloc = PointF.Empty;
  51. using (g.PushClip(new RectangleF(tloc, tsize)))
  52. {
  53. bmp.Renderer.Aliased = true;
  54. tl.AppendLine("No anti-aliasing (worst quality)", tfcap);
  55. tl.Append(text);
  56. tl.PerformLayout(true);
  57. g.DrawTextLayout(tl, tloc);
  58. }
  59. tloc.X += tsize.Width;
  60. using (g.PushClip(new RectangleF(tloc, tsize)))
  61. {
  62. bmp.Renderer.Aliased = false;
  63. bmp.Renderer.SlowAntialiasing = false;
  64. tl.Clear();
  65. tl.AppendLine("Fast anti-aliasing (default quality)", tfcap);
  66. tl.Append(text);
  67. tl.PerformLayout(true);
  68. g.DrawTextLayout(tl, tloc);
  69. }
  70. tloc.X = 0;
  71. tloc.Y += tsize.Height;
  72. using (g.PushClip(new RectangleF(tloc, tsize)))
  73. {
  74. bmp.Renderer.Aliased = false;
  75. bmp.Renderer.SlowAntialiasing = true;
  76. tl.Clear();
  77. tl.AppendLine("Slow anti-aliasing (highest quality)", tfcap);
  78. tl.Append(text);
  79. tl.PerformLayout(true);
  80. g.DrawTextLayout(tl, tloc);
  81. }
  82. return bmp;
  83. }
  84. }
  85. }
  86.