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. {
  29. g.Renderer.Multithreaded = true;
  30.  
  31. var text = Common.Util.LoremIpsum();
  32. var tsize = new SizeF(bmp.Width / 2, bmp.Height / 2);
  33. var pad = 96f / 4;
  34.  
  35. var tfcap = new TextFormat()
  36. {
  37. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "timesbd.ttf")),
  38. FontSize = 16,
  39. };
  40.  
  41. var tl = g.CreateTextLayout();
  42. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
  43. tl.DefaultFormat.FontSize = 14;
  44. tl.TextAlignment = TextAlignment.Justified;
  45. tl.FirstLineIndent = 96 / 2;
  46. tl.ParagraphSpacing = 96 / 8;
  47. tl.MaxWidth = tsize.Width;
  48. tl.MaxHeight = tsize.Height;
  49. tl.MarginAll = pad;
  50.  
  51. var tloc = PointF.Empty;
  52. using (g.PushClip(new RectangleF(tloc, tsize)))
  53. {
  54. bmp.Renderer.Aliased = true;
  55. tl.AppendLine("No anti-aliasing (worst quality)", tfcap);
  56. tl.Append(text);
  57. tl.PerformLayout(true);
  58. g.DrawTextLayout(tl, tloc);
  59. }
  60. tloc.X += tsize.Width;
  61. using (g.PushClip(new RectangleF(tloc, tsize)))
  62. {
  63. bmp.Renderer.Aliased = false;
  64. bmp.Renderer.SlowAntialiasing = false;
  65. tl.Clear();
  66. tl.AppendLine("Fast anti-aliasing (default quality)", tfcap);
  67. tl.Append(text);
  68. tl.PerformLayout(true);
  69. g.DrawTextLayout(tl, tloc);
  70. }
  71. tloc.X = 0;
  72. tloc.Y += tsize.Height;
  73. using (g.PushClip(new RectangleF(tloc, tsize)))
  74. {
  75. bmp.Renderer.Aliased = false;
  76. bmp.Renderer.SlowAntialiasing = true;
  77. tl.Clear();
  78. tl.AppendLine("Slow anti-aliasing (highest quality)", tfcap);
  79. tl.Append(text);
  80. tl.PerformLayout(true);
  81. g.DrawTextLayout(tl, tloc);
  82. }
  83. }
  84. return bmp;
  85. }
  86. }
  87. }
  88.