GcLibsComparison.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 GrapeCity.Documents.Imaging.Skia;
  14. using GrapeCity.Documents.Imaging.Windows;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17.  
  18. namespace DsImagingWeb.Demos
  19. {
  20. // This example demonstrates the differences in fidelity between texts
  21. // rendered on bitmaps using the different DsImaging libraries:
  22. // - DsImaging (cross-platform, package GrapeCity.Documents.Imaging);
  23. // - DsImaging.Skia (cross-platform, package GrapeCity.Documents.Imaging.Skia);
  24. // - DsImaging.Windows (Windows only, package GrapeCity.Documents.Imaging.Windows).
  25. // Note that the results are rather noticeable here because the text is rendered
  26. // using a very small font, and then significantly enlarged when dawn on the
  27. // resulting image generated by the sample. In most real-life applications
  28. // the differences are very subtle if noticeable at all.
  29. public class GcLibsComparison
  30. {
  31. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  32. {
  33. const string text = "Skia is an open source 2D graphics library which provides common APIs that work across a variety of hardware and software platforms.";
  34. int q = 4;
  35. int width = pixelSize.Width / q;
  36. int height = pixelSize.Height / (q * 3);
  37. int margin = 12;
  38. var tf = new TextFormat
  39. {
  40. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "tahoma.ttf")),
  41. FontSize = 10
  42. };
  43. string msg = $"Text with font size {tf.FontSize} rendered using {{0}} and enlarged x{q}:";
  44.  
  45. using var gcBmp = new GcBitmap(width, height, false);
  46. using (var g = gcBmp.CreateGraphics(Color.Transparent))
  47. {
  48. var tl = g.CreateTextLayout();
  49. tl.MaxWidth = width;
  50. tl.MarginAll = margin;
  51. tl.Append(text, tf);
  52. g.DrawTextLayout(tl, PointF.Empty);
  53. }
  54.  
  55. using var gcSkiaBmp = new GcSkiaBitmap(width, height, false);
  56. using (var g = gcSkiaBmp.CreateGraphics(Color.Transparent))
  57. {
  58. var tl = g.CreateTextLayout();
  59. tl.MaxWidth = width;
  60. tl.MarginAll = margin;
  61. tl.Append(text, tf);
  62. g.DrawTextLayout(tl, PointF.Empty);
  63. }
  64.  
  65. GcWicBitmap gcWicBmp = null;
  66. if (GcWicBitmap.IsSupported)
  67. {
  68. gcWicBmp = new GcWicBitmap(width, height, false);
  69. using (var g = gcWicBmp.CreateGraphics(Color.Transparent))
  70. {
  71. var tl = g.CreateTextLayout();
  72. tl.MaxWidth = width;
  73. tl.MarginAll = margin;
  74. tl.Append(text, tf);
  75. g.DrawTextLayout(tl, PointF.Empty);
  76. }
  77. }
  78.  
  79. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque);
  80. using (var g = bmp.CreateGraphics(Color.White))
  81. {
  82. g.Renderer.InterpolationMode = InterpolationMode.NearestNeighbor;
  83. var fs = 16;
  84. var dy = pixelSize.Height / 3;
  85. tf.FontSize = fs;
  86. g.DrawString(string.Format(msg, "GcBitmapGraphics (cross-platform)"), tf, new PointF(margin / 3, margin / 3));
  87. var rc = new RectangleF(0, fs * 2, width * q, height * q - fs * 2);
  88. rc.Inflate(-margin, -margin);
  89. g.DrawImage(gcBmp, rc, null, ImageAlign.StretchImage);
  90. g.DrawRoundRect(rc, margin, Color.MediumVioletRed);
  91.  
  92. g.DrawString(string.Format(msg, "GcSkiaGraphics (cross-platform)"), tf, new PointF(margin / 3, dy + margin / 3));
  93. rc = new RectangleF(0, fs * 2 + dy, width * q, height * q - fs * 2);
  94. rc.Inflate(-margin, -margin);
  95. g.DrawImage(gcSkiaBmp, rc, null, ImageAlign.StretchImage);
  96. g.DrawRoundRect(rc, margin, Color.MediumVioletRed);
  97.  
  98. g.DrawString(string.Format(msg, "GcWicBitmapGraphics (Windows-only)"), tf, new PointF(margin / 3, dy * 2 + margin / 3));
  99. rc = new RectangleF(0, fs * 2 + dy * 2, width * q, height * q - fs * 2);
  100. rc.Inflate(-margin, -margin);
  101. if (gcWicBmp != null)
  102. {
  103. g.DrawImage(gcWicBmp, rc, null, ImageAlign.StretchImage);
  104. }
  105. else
  106. {
  107. var tl = g.CreateTextLayout();
  108. tl.MaxWidth = rc.Width;
  109. tl.MarginAll = margin;
  110. tl.DefaultFormat.Font = tf.Font;
  111. tl.DefaultFormat.FontSize = 16;
  112. tl.DefaultFormat.ForeColor = Color.OrangeRed;
  113. tl.AppendLine(
  114. "Looks like this demo is not running on a Windows system, so WIC (GcWicBitmap, GcWicBitmapGraphics) " +
  115. "is not supported. To see how text rendered using GcWicBitmapGraphics looks, download this demo project " +
  116. "and run it on a Windows system.");
  117. g.DrawTextLayout(tl, rc.Location);
  118. }
  119. g.DrawRoundRect(rc, margin, Color.MediumVioletRed);
  120. }
  121. if (gcWicBmp != null)
  122. gcWicBmp.Dispose();
  123.  
  124. return bmp;
  125. }
  126. }
  127. }
  128.