Skip to main content

Remove EXIF and GPS data from uploaded photos in ASP.NET Core

A photo with a location goes in; the same picture with no location comes out

Take a photo with a phone and the file may record exactly where you stood, along with the time and the phone model. Upload it unchanged and anyone who downloads it can read all of that. A member who posts a picture of their new sofa may have just published their home address, without knowing.

It is on by default

RichTextBox removes this data from every uploaded JPEG, PNG and WebP. You do not need to write anything:

builder.Services.AddRichTextBox();   // StripImageMetadata is true
app.MapRichTextBoxUploads();

EXIF, XMP and IPTC data are removed before validators and the store see the file, so GPS coordinates, camera serial numbers and capture times are not stored anywhere, including in your cloud bucket.

The picture is not re-compressed

Many guides decode a photo and save it again, which costs quality each time. Here the metadata blocks are removed from the file and the pixel data is copied unchanged. A JPEG keeps its exact quality.

The orientation note is kept too. Phones often store a picture sideways with a note saying "turn me". Deleting that note without turning the picture is how portrait photos end up on their side, so it stays.

Turn it off when you need the data

Photographers and archive sites may want their camera data:

builder.Services.AddRichTextBox(options =>
{
    options.StripImageMetadata = false;
});

This is one setting for the whole application. If different users need different behaviour, keep it on and handle the exception in a store of your own.

Check it yourself

Upload a phone photo with location turned on, download it again from your site, and open its properties. On Windows, look under Properties, then Details: the GPS section should be gone. We run the same test against our live site, using a test photo that contains a marker in its metadata.

What this does not cover

A picture can give things away through what it shows, such as a street sign. PDFs and Word files have their own properties, which are not changed, because they are stored as uploaded. And a picture that reaches your site in some other way, such as a data URL pasted into HTML, does not pass through this endpoint.

Read next