ExtractFrames.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 System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample shows how to extract frames from a TIFF image,
  20. // and how to read them as GcBitmap instances.
  21. public class ExtractFrames
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. // Create the resulting image:
  26. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  27.  
  28. // If there is a possibility that we will not fill some areas of the target bitmap
  29. // (as in this case where the number of frames in the TIFF may be less than 4),
  30. // we MUST clear the resulting bitmap as GcBitmap ctor does NOT clear the memory
  31. // allocated for the pixels (to save time, as initializing large bitmaps may be
  32. // very slow):
  33. bmp.Clear();
  34.  
  35. // Sample TIFF image path:
  36. var imagePath = Path.Combine("Resources", "ImagesBis", "BmpWriteTiff3.tiff");
  37.  
  38. // List to hold bitmaps for the TIFF frames:
  39. GcBitmap[] frameBmps;
  40.  
  41. // Read all frames from the TIFF file:
  42. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  43. using (var tr = new GcTiffReader(stm))
  44. {
  45. frameBmps = new GcBitmap[tr.Frames.Count];
  46. for (int i = 0; i < tr.Frames.Count; ++i)
  47. {
  48. frameBmps[i] = tr.Frames[i].ToGcBitmap();
  49. frameBmps[i].Opaque = opaque;
  50. }
  51. }
  52.  
  53. // Resize and render the frames' bitmaps into the resulting image:
  54. int w = pixelSize.Width / 2;
  55. int h = pixelSize.Height / 2;
  56. if (frameBmps.Length > 0)
  57. using (var tbmp = frameBmps[0].Resize(w, h))
  58. bmp.BitBlt(tbmp, 0, 0);
  59. if (frameBmps.Length > 1)
  60. using (var tbmp = frameBmps[1].Resize(w, h))
  61. bmp.BitBlt(tbmp, w, 0);
  62. if (frameBmps.Length > 2)
  63. using (var tbmp = frameBmps[2].Resize(w, h))
  64. bmp.BitBlt(tbmp, 0, h);
  65. if (frameBmps.Length > 3)
  66. using (var tbmp = frameBmps[3].Resize(w, h))
  67. bmp.BitBlt(tbmp, w, h);
  68.  
  69. // Dispose the frame bitmaps:
  70. foreach (var tbmp in frameBmps)
  71. tbmp.Dispose();
  72.  
  73. // Add borders between the quadrants, and captions for each:
  74. using (var g = bmp.CreateGraphics())
  75. {
  76. var foreColor = Color.Yellow;
  77. var backColor = Color.Blue;
  78. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  79. var lineh = 2;
  80. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
  81. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
  82. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  83. var th = g.MeasureString("QWERTY", tf).Height;
  84. if (frameBmps.Length > 0)
  85. g.DrawString(" Frame 0 ", tf, new PointF(0, h - th + lineh));
  86. if (frameBmps.Length > 1)
  87. g.DrawString(" Frame 1 ", tf, new PointF(w + lineh, h - th + lineh));
  88. if (frameBmps.Length > 2)
  89. g.DrawString(" Frame 2 ", tf, new PointF(0, h * 2 + lineh - th + lineh));
  90. if (frameBmps.Length > 3)
  91. g.DrawString(" Frame 3 ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
  92. }
  93.  
  94. return bmp;
  95. }
  96. }
  97. }
  98.