InterpolationModes.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This example shows how SaveAsImageOptions.InterpolationMode affects the result
  17. // when a PDF is saved to raster images using SaveAsJpeg or other SaveAsXXX methods.
  18. public class InterpolationModes
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. // Small image of a QRCode that will be enlarged in PDFs that are then saved as JPEGs:
  23. using var image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "QRCode-57x57.png"));
  24.  
  25. // Create 4 PDFs, each is one page on which the small image is enlarged,
  26. // save each PDF as JPEG with different interpolation modes specified in SaveAsImageOptions:
  27. var tmpNearestNeighbor = Path.GetTempFileName();
  28. MakePDF(image, InterpolationMode.NearestNeighbor).SaveAsJpeg(tmpNearestNeighbor, null,
  29. new SaveAsImageOptions() { InterpolationMode = InterpolationMode.NearestNeighbor });
  30. using var imgNearestNeighbor = GCDRAW.Image.FromFile(tmpNearestNeighbor);
  31. var tmpLinear = Path.GetTempFileName();
  32. MakePDF(image, InterpolationMode.Linear).SaveAsJpeg(tmpLinear, null,
  33. new SaveAsImageOptions() { InterpolationMode = InterpolationMode.Linear });
  34. using var imgLinear = GCDRAW.Image.FromFile(tmpLinear);
  35. var tmpCubic = Path.GetTempFileName();
  36. MakePDF(image, InterpolationMode.Cubic).SaveAsJpeg(tmpCubic, null,
  37. new SaveAsImageOptions() { InterpolationMode = InterpolationMode.Cubic });
  38. using var imgCubic = GCDRAW.Image.FromFile(tmpCubic);
  39. var tmpDownscale = Path.GetTempFileName();
  40. MakePDF(image, InterpolationMode.Downscale).SaveAsJpeg(tmpDownscale, null,
  41. new SaveAsImageOptions() { InterpolationMode = InterpolationMode.Downscale });
  42. using var imgDownscale = GCDRAW.Image.FromFile(tmpDownscale);
  43.  
  44. // Draw each JPEG at its natural size onto a page of the resulting PDF:
  45. var doc = new GcPdfDocument();
  46. var page = doc.NewPage();
  47. page.Graphics.DrawImage(imgNearestNeighbor, page.Bounds, null, ImageAlign.Default);
  48. page = doc.NewPage();
  49. page.Graphics.DrawImage(imgLinear, page.Bounds, null, ImageAlign.Default);
  50. page = doc.NewPage();
  51. page.Graphics.DrawImage(imgCubic, page.Bounds, null, ImageAlign.Default);
  52. page = doc.NewPage();
  53. page.Graphics.DrawImage(imgDownscale, page.Bounds, null, ImageAlign.Default);
  54. // Save the resulting PDF:
  55. doc.Save(stream);
  56. // CLeanup:
  57. File.Delete(tmpNearestNeighbor);
  58. File.Delete(tmpLinear);
  59. File.Delete(tmpCubic);
  60. File.Delete(tmpDownscale);
  61.  
  62. // Done:
  63. return doc.Pages.Count;
  64. }
  65.  
  66. // InterpolationMode parameter here is only to show in the resulting PDF,
  67. // GcPdfGraphics only supports InterpolationMode.NearestNeighbor:
  68. public GcPdfDocument MakePDF(GCDRAW.Image image, InterpolationMode imode)
  69. {
  70. var tfCaption = new TextFormat
  71. {
  72. Font = StandardFonts.Times,
  73. FontSize = 20,
  74. };
  75. var tf = new TextFormat
  76. {
  77. Font = StandardFonts.Times,
  78. FontSize = 18,
  79. };
  80.  
  81. var doc = new GcPdfDocument();
  82. var page = doc.NewPage();
  83. var g = page.Graphics;
  84.  
  85. var xpad = 36;
  86. var ypad = 36;
  87. var ip = new PointF(xpad, ypad);
  88. g.DrawString($"SaveAsImageOptions.InterpolationMode is {imode}", tfCaption, ip);
  89. ip.Y *= 2.5f;
  90.  
  91. // Draw the original with the original size:
  92. g.DrawImage(image, new RectangleF(ip.X, ip.Y, image.Width, image.Height), null, ImageAlign.Default);
  93. g.DrawString($"⟵ Original image ({image.Width} by {image.Height} pixels)", tf, new PointF(ip.X * 2 + image.Width, ip.Y));
  94. ip.Y += image.Height + ypad;
  95.  
  96. // Enlarge the original small image by a factor of 8:
  97. var f = 8;
  98. int twidth = image.Width * f;
  99. int theight = image.Height * f;
  100.  
  101. // Draw the enlarged image:
  102. g.DrawImage(image, new RectangleF(ip.X, ip.Y, twidth, theight), null, ImageAlign.StretchImage);
  103. g.DrawString($"Image enlarged to {twidth} by {theight} pixels:", tf, new PointF(ip.X, ip.Y - ypad));
  104.  
  105. return doc;
  106. }
  107. }
  108. }
  109.