HomeAuthorContact
dotNET
How to remove metadata from images in .NET?
Laurent Egbakou
Laurent Egbakou
February 20, 2022
2 min
Our work may have helped you ?
Support Us

Table Of Contents

01
What is photo metadata?
02
Why remove metadata from images?
03
Which tools to use?
04
Remove metadata using the NuGet package Magick.NET-Q16-AnyCPU
05
Installation
06
Remove metadata using the NuGet package SixLabors.ImageSharp
07
Support
08
Useful links

What is photo metadata?

“Photo metadata is a set of data describing and providing information about rights and administration of an image.” - iptc.org

In other words, metadata is information stored with a photo. It includes information about the device that took the picture, the location, the camera settings, copyrights, and more.

In this blog post, I am not going to share with you how to read this information but how to remove it.

Why remove metadata from images?

As metadata can contain information about the GPS location and device that took the pic, you won't probably let people see other users' location using their images in your application. Imagine you implement your social media, where people can upload pics and watch others users' pics. If metadata is not deleted from the uploaded images, this might cause privacy concerns for your application's users.

Now let's see how he can do that in a . NET.

Which tools to use?

I found some open source NuGet packages which can be used to read and also remove metadata from images.

GitHub ProjectNuGet LinkCan read metadata ?Can remove metadata ?
dlemstra/Magick.NETMagick.NET-Q16-AnyCPUYesYes
SixLabors/ImageSharpSixLabors.ImageSharpYesYes

I have already tried these libraries and I found them very interesting. They give you an option to save the image in a different format from the original after metadata is removed. So I will show you how to use each of them.

Remove metadata using the NuGet package Magick.NET-Q16-AnyCPU

Installation

Magick.NET-Q16-AnyCPU can be found here on NuGet and can be installed by copying and pasting the following command into your Package Manager Console within Visual Studio (Tools > NuGet Package Manager > Package Manager Console).

Install-Package Magick.NET-Q16-AnyCPU

Alternatively, if you're using .NET Core then you can install Magick.NET-Q16-AnyCPU via the CLI with the following command:

dotnet add package Magick.NET-Q16-AnyCPU

Usage

  1. Load the image
using ImageMagick; using var image = new MagickImage("path_to_the_image/image.jpg"); // Or use Sytem.IO.Stream instead of path using var image = new MagickImage(streamObject);
  1. Load Metadata: EXIF, IPTC, and XMP profiles
// Exif profile: Camera settings, GPS location, Date, Time Zone, ... etc var exifProfile = heicImage.GetExifProfile(); // IPTC profile: Copyright, Permissions and licenses, Creator's information and contact details, Rights usage terms, ...etc var iptcProfile = heicImage.GetIptcProfile(); // Xmp profile: IPTC with additionnal information var xmpProfile = heicImage.GetXmpProfile();
  1. Remove metadata if exists
if(exifProfile != null) image.RemoveProfile(exifProfile); if(iptcProfile != null) image.RemoveProfile(iptcProfile); if(xmpProfile != null) image.RemoveProfile(xmpProfile);
  1. Finally, save the image without metadata
image.Format = MagickFormat.Png; // Change the output format if needed await image.WriteAsync("images/user-000001.png"); // Instead of saving the image, if you want to return an array of byte byte[] data = image.ToByteArray();

Complete code

using ImageMagick; // Load the image using var image = new MagickImage("path_to_the_image/image.jpg"); // Or use Sytem.IO.Stream instead of path using var image = new MagickImage(streamObject); // Exif profile contain information like the device that took the photo, Camera settings, GPS location, Date, Time Zone, ... etc var exifProfile = heicImage.GetExifProfile(); // IPTC profile: Copyright, Permissions and licenses, Creator's information and contact details, Rights usage terms, ...etc var iptcProfile = heicImage.GetIptcProfile(); // Xmp profile: IPTC with additionnal information var xmpProfile = heicImage.GetXmpProfile(); // Remove metadata if exists if(exifProfile != null) image.RemoveProfile(exifProfile); if(iptcProfile != null) image.RemoveProfile(iptcProfile); if(xmpProfile != null) image.RemoveProfile(xmpProfile); // Finally save the image without metadata image.Format = MagickFormat.Png; // Change the output format if needed await image.WriteAsync("images/user-000001.png"); // Instead of saving the image, if you want to return an array of byte byte[] data = image.ToByteArray();

Please note: In case you are manipulating HEIC format, the WriteAsync method does not support it, you will need to convert it to another format (png, jpg, jpeg,... for example).

Remove metadata using the NuGet package SixLabors.ImageSharp

Installation

Just like we installed the previous NuGet package, SixLabors.ImageSharp can be installed through the Package Manager Console.

Install-Package SixLabors.ImageSharp

or via .NET CLI

dotnet add package SixLabors.ImageSharp

Usage

  1. Load the image
using SixLabors.ImageSharp; using var image = await Image.LoadAsync("path_to_the_image/image.jpg"); // Or use Sytem.IO.Stream instead of path using var image = await Image.LoadAsync(streamObject);
  1. Remove Metadata: EXIF, IPTC, and XMP profiles
image.Metadata.ExifProfile = null; image.Metadata.IptcProfile = null; image.Metadata.XmpProfile = null;
  1. Save the image without metadata
using SixLabors.ImageSharp.Formats.Jpeg; await image.SaveAsync("images/user-000001.jpg"); // Convert the image to a different format await image.SaveAsPngAsync("..."); await image.SaveAsGifAsync("..."); await image.SaveAsJpegAsync("..."); await image.SaveAsPbmAsync("..."); await image.SaveAsTiffAsync("..."); await image.SaveAsTgaAsync("..."); await image.SaveAsWebpAsync("..."); await image.SaveAsBmpAsync("..."); // Or convert the output image to base64 string var imageToBase64String = image.ToBase64String(JpegFormat.Instance); // alternatively, write the image to an output stream as a JPEG file using var stream = new MemoryStream(); await image.SaveAsJpegAsync(stream);

Complete code

using SixLabors.ImageSharp; // Load the image using var image = await Image.LoadAsync("path_to_the_image/image.jpg"); // Remove metadata image.Metadata.ExifProfile = null; image.Metadata.IptcProfile = null; image.Metadata.XmpProfile = null; // Save the image without metadata await image.SaveAsync("images/user-000001.jpg");

SixLabors.ImageSharp supports only Webp, PNG, TIFF, GIF, TGA, BMP, PBM, and JPEG formats. This means you cannot load an image of any other format than those I have listed.

Support

If you find this blog post useful, please share it on your favorite social media. Don't forget to follow me on GitHub and Twitter. To send me a message, please use the contact form or DM me on Twitter.


Tags

#image#metadata#exif#csharp

Share


Laurent Egbakou

Laurent Egbakou

Microsoft MVP | Founder

Microsoft MVP | Software Engineer ☁️ | Blogger | Open source contributor

Expertise

.NET
Go
Python
Cloud
Open Source

Social Media

linkedintwitterwebsitegithub
Microsoft MVPAzure Developer Associate Badge

Related Posts

File size from Base64 string
Calculate a file size from Base64 string
February 20, 2022
2 min
RESTCountries.NET
Open Source
Getting started with RESTCountries.NET
December 19, 2021
2 min
© 2024, All Rights Reserved.
Powered By @lioncoding

French Content 🔜

Quick Links

Advertise with usAbout UsContact UsHire Us

Social Media