Skip to content

Instantly share code, notes, and snippets.

@phillip-haydon
Created August 27, 2014 18:30
Show Gist options
  • Save phillip-haydon/d5d18517ec923b23e158 to your computer and use it in GitHub Desktop.
Save phillip-haydon/d5d18517ec923b23e158 to your computer and use it in GitHub Desktop.
using (var exifStream = new MemoryStream())
using (var imageFactory = new ImageFactory())
{
file.Value.Position = 0;
file.Value.CopyTo(exifStream);
exifStream.Position = 0;
var exifData = imageFactory.Load(exifStream)
.GetExifTags();
foreach (var tag in exifData)
{
photo.AddImageProperties(tag.Name, tag.Value, tag.RawValue);
}
}
@JimBobSquarePants
Copy link

So are file and photo both Image files? Would AddImageProperties be an extension to that type?

ImageFactory maintains an image internally so you'd probably be better to spin up another instance of that and add the EXIF tags to that instance.

Something like

ExifData exifData;
byte[] originalBytes = File.ReadAllBytes(originalFile);
byte[] copyBytes = File.ReadAllBytes(copyFile);
using (var instream = new MemoryStream(originalBytes))
using (var imageFactory = new ImageFactory())
{ 
    // Copy the data from the original as a property so 
    // we can keep the fluent API
    exifData = imageFactory.Load(exifStream)
                           .ExifTags;
}

// Now load up and copy what we want
using (var copyStream = new MemoryStream(copyBytes))
using (var outStream = new MemoryStream())
using (var imageFactory2 = new ImageFactory())
{ 
    imageFactory2.Load(copyStream)

    // Replaces tags.
    foreach (var tag in exifData.Tags)
    {
        imageFactory2.exifData.Update(tag.Name, tag.Value, tag.RawValue);
    }

    imageFactory2.Save(outStream);
}

// Do something with the outStream.

You could possibly simplify that also by using the Load(string) and Save(sting) overloads

@phillip-haydon
Copy link
Author

OH, I'm using NancyFX so am currently running this stuff when the file is uploaded.

So file happens to be the file uploaded, but I have to copy it into a MemoryStream to pass into ImageFactory.

Also I saved the raw file prior to this which is why i reset the stream. It's just fluff code.


So you want to be able to set value as well as get them? Right now I'm only focusing on getting the values out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment