Remove GPS location from images or photos (EXIF Metadata Removal)


/ Published in: C#
Save to your folder(s)

The code uses [GroupDocs.Metadata for .NET](http://www.groupdocs.com/dot-net/document-metadata-library) API for removing GPS location from images / photos.
-Download API from [Nuget](https://www.nuget.org/packages/groupdocs-metadata-dotnet/).
-Download complete code examples from [Github](https://github.com/groupdocs-metadata/GroupDocs.Metadata-for-.NET/blob/master/Examples/).


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using GroupDocs.Metadata.Tools;
  6. using GroupDocs.Metadata.Standards.Exif;
  7. using System.IO;
  8.  
  9. namespace GroupDocs.Metadata.Examples.Utilities.CSharp
  10. {
  11. //ExStart:PhotoCleaner
  12. public class PhotoCleaner
  13. {
  14. // absolute path to the GroupDocs.Metadata license file.
  15. private const string LicensePath = @"GroupDocs.Metadata.lic";
  16.  
  17. // absolute path to the photos directory.
  18. public string CleanerPath { get; set; }
  19.  
  20. static PhotoCleaner()
  21. {
  22. /* set product license
  23.   * uncomment following function if you have product license
  24.   * */
  25. //SetInternalLicense();
  26. }
  27.  
  28. public PhotoCleaner(string cleanerPath)
  29. {
  30. // check if directory exists
  31. if (!Directory.Exists(Common.MapSourceFilePath( cleanerPath)))
  32. {
  33. throw new DirectoryNotFoundException("Directory not found: " + cleanerPath);
  34. }
  35. // set property
  36. this.CleanerPath = cleanerPath;
  37. }
  38. /// <summary>
  39. /// Applies the product license
  40. /// </summary>
  41. private static void SetInternalLicense()
  42. {
  43. License license = new License();
  44. license.SetLicense(LicensePath);
  45. }
  46. /// <summary>
  47. /// Removes GPS data and updates the image files in a directory
  48. /// </summary>
  49. public void RemoveExifLocation()
  50. {
  51. // Map directory in source folder
  52. string sourceDirectoryPath = Common.MapSourceFilePath(this.CleanerPath);
  53.  
  54. // get array of file in specific directory
  55. string[] files = Directory.GetFiles(sourceDirectoryPath);
  56.  
  57. foreach (string path in files)
  58. {
  59. // get EXIF data if exists
  60. ExifMetadata exifMetadata = (ExifMetadata)MetadataUtility.ExtractSpecificMetadata(path, MetadataType.EXIF);
  61.  
  62. if (exifMetadata != null)
  63. {
  64. ExifInfo exifInfo = exifMetadata.Data;
  65.  
  66. if (exifInfo.GPSData != null)
  67. {
  68. // set altitude, latitude and longitude to null values
  69. exifInfo.GPSData.Altitude = null;
  70. exifInfo.GPSData.Latitude = null;
  71. exifInfo.GPSData.LatitudeRef = null;
  72. exifInfo.GPSData.Longitude = null;
  73. exifInfo.GPSData.LongitudeRef = null;
  74. }
  75.  
  76. // and update file
  77. MetadataUtility.UpdateMetadata(path, exifMetadata);
  78. }
  79. }
  80. Console.WriteLine("Press any key to exit.");
  81. }
  82. }
  83. //ExEnd:PhotoCleaner
  84. }

URL: https://github.com/groupdocs-metadata/GroupDocs.Metadata-for-.NET/blob/master/Examples/GroupDocs.Metadata.Examples.CSharp/Utilities/PhotoCleaner.cs

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.