EnlargeQRCode.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 results of enlarging a small (57 by 57 pixels) image
  19. // of a QRCode using the 4 available interpolation modes.
  20. // While for most images (such as portraits or landscapes), Linear or Cubic
  21. // interpolation modes would normally yield better-looking results,
  22. // for images like QRCodes or barcodes using the NearestNeighbor or
  23. // Downscale modes is more appropriate.
  24. //
  25. // See EnlargeImage for an example where a small photo (portrait of a woman)
  26. // is enlarged using the same 4 interpolation modes.
  27. public class EnlargeQRCode
  28. {
  29. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  30. {
  31. // Create and clear the target bitmap:
  32. var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  33. targetBmp.Clear(Color.Transparent);
  34.  
  35. const int fontSize = 16;
  36. var xpad = (int)(dpi * .5f);
  37. var ypad = (int)(dpi * .7f);
  38. TextFormat tf = new TextFormat
  39. {
  40. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  41. FontSize = fontSize,
  42. };
  43.  
  44. // Small image of a QRCode:
  45. using var origBmp = new GcBitmap();
  46. using (var stm = File.OpenRead(Path.Combine("Resources", "ImagesBis", "QRCode-57x57.png")))
  47. origBmp.Load(stm);
  48.  
  49. // Make sure opaqueness of the original bitmap matches the target:
  50. origBmp.Opaque = targetBmp.Opaque;
  51.  
  52. var ip = new Point(xpad, ypad);
  53.  
  54. // Draw the original with the original size:
  55. targetBmp.BitBlt(origBmp, ip.X, ip.Y);
  56. using (var g = targetBmp.CreateGraphics(null))
  57. g.DrawString($"⟵ Original image ({origBmp.PixelWidth} by {origBmp.PixelHeight} pixels)", tf, new PointF(xpad * 2 + origBmp.Width, ip.Y));
  58. ip.Y += origBmp.PixelHeight + ypad;
  59.  
  60. // We are going to enlarge the original small image by a factor of 6:
  61. var f = 6;
  62. int twidth = origBmp.PixelWidth * f;
  63. int theight = origBmp.PixelHeight * f;
  64.  
  65. // Enlarge and draw 4 copies of the image using the 4 available interpolation modes:
  66. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
  67. targetBmp.BitBlt(bmp, ip.X, ip.Y);
  68. drawCaption("InterpolationMode.NearestNeighbor", ip.X, ip.Y + theight);
  69.  
  70. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
  71. targetBmp.BitBlt(bmp, ip.X + twidth + xpad, ip.Y);
  72. drawCaption("InterpolationMode.Cubic", ip.X + twidth + xpad, ip.Y + theight);
  73.  
  74. ip.Y += theight + ypad;
  75.  
  76. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear))
  77. targetBmp.BitBlt(bmp, ip.X, ip.Y);
  78. drawCaption("InterpolationMode.Linear", ip.X, ip.Y + theight);
  79.  
  80. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale))
  81. targetBmp.BitBlt(bmp, ip.X + twidth + xpad, ip.Y);
  82. drawCaption("InterpolationMode.Downscale", ip.X + twidth + xpad, ip.Y + theight);
  83. //
  84. void drawCaption(string caption, float x, float y)
  85. {
  86. using var g = targetBmp.CreateGraphics(null);
  87. g.DrawString(caption, tf, new PointF(x, y));
  88. }
  89. return targetBmp;
  90. }
  91. }
  92. }
  93.