CreateThumbnails.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 demonstrates how to resize (downscale) an existing image
  19. // to create a thumbnail, using different interpolation modes.
  20. public class DownscaleImage
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var origImagePath = Path.Combine("Resources", "Images", "minerva.jpg");
  25. // Create and fill the target bitmap:
  26. var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  27. using (var g = targetBmp.CreateGraphics(Color.FromArgb(unchecked((int)0xff004d99))))
  28. {
  29. // creating a graphics with a fill color does what we need
  30. }
  31. const int fontSize = 16, fpad = 4, xpad = 10, ypad = 3;
  32. TextFormat tf = new TextFormat
  33. {
  34. ForeColor = Color.FromArgb(unchecked((int)0xffffcc00)),
  35. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  36. FontSize = fontSize,
  37. };
  38. using (var origBmp = new GcBitmap())
  39. {
  40. using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  41. origBmp.Load(stm);
  42.  
  43. // Bitwise copy the original image 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(fpad, origBmp.Height + fpad));
  47. int dx = (int)origBmp.Width + xpad;
  48.  
  49. // Scale down the image using the various interpolation modes,
  50. // and render those on the right of the original:
  51. float f = (origBmp.Height - ypad * 3) / origBmp.Height / 4;
  52. int twidth = (int)Math.Round(origBmp.Width * f);
  53. int theight = (int)Math.Round(origBmp.Height * f);
  54.  
  55. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
  56. targetBmp.BitBlt(bmp, dx, 0);
  57. drawCaption("InterpolationMode.NearestNeighbor", dx, 0);
  58.  
  59. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear))
  60. targetBmp.BitBlt(bmp, dx, theight + ypad);
  61. drawCaption("InterpolationMode.Linear", dx, theight + ypad);
  62.  
  63. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
  64. targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2);
  65. drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2);
  66.  
  67. using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale))
  68. targetBmp.BitBlt(bmp, dx, (theight + ypad) * 3);
  69. drawCaption("InterpolationMode.Downscale", dx, (theight + ypad) * 3);
  70. //
  71. void drawCaption(string caption, float x, float y)
  72. {
  73. using (var g = targetBmp.CreateGraphics(null))
  74. g.DrawString(caption, tf, new PointF(x + twidth + fpad, y + fpad));
  75. }
  76. }
  77. return targetBmp;
  78. }
  79. }
  80. }
  81.