ClearExif.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 GrapeCity.Documents.Imaging.Exif;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // Clear all Exif tags found in an image.
  20. // This sample is almost identical to @(ShowExif} but prior to printing
  21. // Exif tags found in the image it clears all those tags.
  22. // The expected result is that no tags are found.
  23. public class ClearExif
  24. {
  25. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  26. {
  27. var pad = 20;
  28. var side = pixelSize.Width / 3;
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  30. using (var g = bmp.CreateGraphics(Color.White))
  31. using (var testImage = new GcBitmap(Path.Combine("Resources", "ImagesBis", "fire.jpg")))
  32. {
  33. // Remove all existing values from the image's Exif profile:
  34. testImage.ExifProfile.Clear();
  35. // Note: Individual tags can be removed via testImage.ExifProfile.RemoveValue(tag);
  36.  
  37. // The remaining code is same as in ShowExif:
  38. var tl = g.CreateTextLayout();
  39. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
  40. tl.DefaultFormat.FontSize = 11;
  41. tl.DefaultTabStops = 160;
  42. tl.MarginTop = pad;
  43. tl.MarginBottom = pad;
  44. tl.MarginLeft = pad * 2 + side;
  45. tl.MarginRight = pad;
  46. tl.MaxWidth = pixelSize.Width;
  47. AppendExifData(tl, testImage.ExifProfile);
  48. g.DrawImage(testImage, new RectangleF(pad, pad, side, side), null, ImageAlign.ScaleImage);
  49. if (tl.Lines.Count == 0)
  50. tl.AppendLine("No EXIF tags found.");
  51. g.DrawTextLayout(tl, PointF.Empty);
  52. }
  53. return bmp;
  54. }
  55.  
  56. void AppendExifData(TextLayout tl, ExifProfile ep)
  57. {
  58. Append(tl, ep, ExifTag.Make, ep.Make);
  59. Append(tl, ep, ExifTag.Model, ep.Model);
  60. Append(tl, ep, ExifTag.XResolution, ep.XResolution?.ToString());
  61. Append(tl, ep, ExifTag.YResolution, ep.YResolution?.ToString());
  62. Append(tl, ep, ExifTag.ResolutionUnit, ep.ResolutionUnit.ToString());
  63. Append(tl, ep, ExifTag.Software, ep.Software);
  64. Append(tl, ep, ExifTag.DateTime, ep.DateTimeRaw);
  65. Append(tl, ep, ExifTag.Artist, ep.Artist);
  66. Append(tl, ep, ExifTag.Copyright, ep.Copyright);
  67. Append(tl, ep, ExifTag.ExposureTime, ep.ExposureTime?.ToString());
  68. Append(tl, ep, ExifTag.FNumber, ep.FNumber?.ToString());
  69. Append(tl, ep, ExifTag.ExposureProgram, ep.ExposureProgram.ToString());
  70. Append(tl, ep, ExifTag.PhotographicSensitivity, ep.PhotographicSensitivity?.ToString());
  71. Append(tl, ep, ExifTag.ExifVersion, ep.ExifVersion);
  72. Append(tl, ep, ExifTag.DateTimeOriginal, ep.DateTimeOriginalRaw);
  73. Append(tl, ep, ExifTag.DateTimeDigitized, ep.DateTimeDigitizedRaw);
  74. Append(tl, ep, ExifTag.ShutterSpeedValue, ep.ShutterSpeedValue?.ToString());
  75. Append(tl, ep, ExifTag.ApertureValue, ep.ApertureValue?.ToString());
  76. Append(tl, ep, ExifTag.ExposureBiasValue, ep.ExposureBiasValue?.ToString());
  77. Append(tl, ep, ExifTag.MaxApertureValue, ep.MaxApertureValue?.ToString());
  78. Append(tl, ep, ExifTag.MeteringMode, ep.MeteringMode.ToString());
  79. Append(tl, ep, ExifTag.LightSource, ep.LightSource.ToString());
  80. Append(tl, ep, ExifTag.Flash, ep.Flash.ToString());
  81. Append(tl, ep, ExifTag.FocalLength, ep.FocalLength?.ToString());
  82. // First 8 symbols in UserComment specify encoding:
  83. Append(tl, ep, ExifTag.UserComment, ep.UserComment?.Substring(8));
  84. Append(tl, ep, ExifTag.SubsecTimeOriginal, ep.SubsecTimeOriginal?.ToString());
  85. Append(tl, ep, ExifTag.SubsecTimeDigitized, ep.SubsecTimeDigitized?.ToString());
  86. Append(tl, ep, ExifTag.ColorSpace, ep.ColorSpace.ToString());
  87. Append(tl, ep, ExifTag.SensingMethod, ep.SensingMethod.ToString());
  88. Append(tl, ep, ExifTag.FileSource, ep.FileSource.ToString());
  89. Append(tl, ep, ExifTag.SceneType, ep.SceneType?.ToString());
  90. Append(tl, ep, ExifTag.CFAPattern, ep[ExifTag.CFAPattern]?.ToString());
  91. Append(tl, ep, ExifTag.CustomRendered, ep.CustomRendered.ToString());
  92. Append(tl, ep, ExifTag.ExposureMode, ep.ExposureMode.ToString());
  93. Append(tl, ep, ExifTag.WhiteBalance, ep.WhiteBalance.ToString());
  94. Append(tl, ep, ExifTag.DigitalZoomRatio, ep.DigitalZoomRatio?.ToString());
  95. Append(tl, ep, ExifTag.FocalLengthIn35mmFilm, ep.FocalLengthIn35mmFilm?.ToString());
  96. Append(tl, ep, ExifTag.SceneCaptureType, ep.SceneCaptureType.ToString());
  97. Append(tl, ep, ExifTag.Contrast, ep.Contrast.ToString());
  98. Append(tl, ep, ExifTag.Saturation, ep.Saturation.ToString());
  99. Append(tl, ep, ExifTag.Sharpness, ep.Sharpness.ToString());
  100. Append(tl, ep, ExifTag.SubjectDistanceRange, ep.SubjectDistanceRange.ToString());
  101. Append(tl, ep, ExifTag.BodySerialNumber, ep.BodySerialNumber);
  102. Append(tl, ep, ExifTag.LensSpecification, ep[ExifTag.LensSpecification]?.ToString());
  103. Append(tl, ep, ExifTag.LensModel, ep.LensModel);
  104. }
  105.  
  106. void Append(TextLayout tl, ExifProfile ep, ExifTag tag, string text)
  107. {
  108. if (ep.HasValue(tag))
  109. {
  110. tl.AppendLine(tag.ToString() + '\t' + text);
  111. }
  112. }
  113. }
  114. }
  115.