EnlargeImage.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 how to enlarge a loaded image
  19. // using different interpolation modes.
  20. public class EnlargeImage
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var origImagePath = Path.Combine("Resources", "Stock", "woman-window-small.jpg");
  25. // Create and clear the target bitmap:
  26. var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  27. targetBmp.Clear(Color.Transparent);
  28.  
  29. const int fontSize = 16, fpad = 4, xpad = 4, ypad = 3;
  30. TextFormat tf = new TextFormat
  31. {
  32. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  33. FontSize = fontSize,
  34. };
  35. using (var origBmp = new GcBitmap())
  36. {
  37. using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  38. origBmp.Load(stm);
  39.  
  40. // Make sure opaqueness of the original bitmap matches the target:
  41. origBmp.Opaque = targetBmp.Opaque;
  42.  
  43. // Draw the original with the original size:
  44. targetBmp.BitBlt(origBmp, 0, 0);
  45. using (var g = targetBmp.CreateGraphics(null))
  46. g.DrawString($"⟵ Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, new PointF(origBmp.Width + fpad, fpad));
  47. int dy = (int)origBmp.Height + ypad;
  48.  
  49. // Enlarge image so that we can have 4 enlarged tiled images preserving aspect ratio:
  50. var f = Math.Min(
  51. (targetBmp.Width - xpad) / origBmp.Width / 2,
  52. (targetBmp.Height - ypad - dy) / origBmp.Height / 2);
  53. int twidth = (int)(origBmp.Width * f);
  54. int theight = (int)(origBmp.Height * f);
  55.  
  56. // Enlarge and draw 4 copies using the 4 available interpolation modes:
  57. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
  58. targetBmp.BitBlt(bmp, 0, dy);
  59. drawCaption("InterpolationMode.NearestNeighbor", 0, dy);
  60.  
  61. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear))
  62. targetBmp.BitBlt(bmp, 0, dy + theight + ypad);
  63. drawCaption("InterpolationMode.Linear", 0, dy + theight + ypad);
  64.  
  65. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
  66. targetBmp.BitBlt(bmp, twidth + xpad, dy);
  67. drawCaption("InterpolationMode.Cubic", twidth + xpad, dy);
  68.  
  69. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale))
  70. targetBmp.BitBlt(bmp, twidth + xpad, dy + theight + ypad);
  71. drawCaption("InterpolationMode.Downscale", twidth + xpad, dy + theight + ypad);
  72. //
  73. void drawCaption(string caption, float x, float y)
  74. {
  75. using (var g = targetBmp.CreateGraphics(null))
  76. g.DrawString(caption, tf, new RectangleF(x + fpad, y + theight - fontSize - fpad * 2, twidth - fpad * 2, fontSize + fpad * 2));
  77. }
  78. }
  79. return targetBmp;
  80. }
  81. }
  82. }
  83.