Search code examples
.netumbracogdi+

Umbraco/.NET - using GDI+ to access image metadata, file not found exception


I'm trying to access EXIF metadata for images in my Umbraco Media Library using System.Drawing.Image.FromFile and whilst I can get my code to work passing a path for a local file on my HDD (e.g. c:\example.jpg) I get a file not found exception if I pass the URL for an item in my media library. I have tried concatenating the URL with http://localhost:xxxx, and I get an error that the format is incorrect. I've tried this with a full Url of an image hosted online and get same, which leads me to suspect that FromFile doesn't accept an Url. I can't find any documentation to support this theory though, and it seems, dumb?

TL;DR why does a path work for System.Drawing.Image.FromFile but not a Url?

Here's my code:

var testImage = Model.Value<IPublishedContent>("testImage");
System.Drawing.Image image = System.Drawing.Image.FromFile(testImage.Url);

Solution

  • I'd venture a guess that Image.FromFile needs a proper file path and not a URL like the one you're providing.

    Have a look at this question: ASP.NET Get physical filepath from URL and see if one of the provided options work for you.

    So you'd do something like

    var testImage = Model.Value<IPublishedContent>("testImage");
    var physicalPath = Server.MapPath(testImage.Url);
    System.Drawing.Image image = System.Drawing.Image.FromFile(physicalPath);
    

    (I didn't test that, but I hope it gets you somewhere closer)