I am loading a jpeg and I need to save & read a few custom properties from the image file so I know what image processing I did on the image. The values should be saved to the jpeg file and when read later should be available. looking on the available options, I see EXIF does not let me add custom data (I do not want to use something existing because it might be in use by the jpeg creator and I need to preserve existing exif data), I have also looked at XMP data and saw that I could probably add some package but I could not find any sample on how to do that.
Any advice on how to achieve this? I am using .NET version of imaging
Hi @DanAvni
I will send you examples soon
@DanAvni
You your purposes you can use
- ExifData.MakerNoteRawData - just write a byte array to the tail of the available data, at the beginning and end of which there will be some kind of marker to understand that this is your data
, then you can read the last bytes and determine the presence of your marker in the file
- XmpData
using (Image image = Image.Load("test.jpg"))
{
XmpPacketWrapper wrapper = null;
if (image is IHasXmpData)
{
wrapper = ((IHasXmpData)image).XmpData;
}
if (wrapper == null)
{
wrapper = new XmpPacketWrapper();
}
// Your website with a description of XMP data may be fake
string yourUrlWithDescription = "http://yoursite.com/DanAvni/elements/1/";
// Your own prefix for each value you use in your package
string yourOwnPrefix = "DanAvni";
XmpPackage myPackage = wrapper.GetPackage(yourUrlWithDescription);
if (myPackage == null)
{
myPackage = new XmpPackage(yourOwnPrefix, yourUrlWithDescription);
wrapper.AddPackage(myPackage);
}
// You MUST set your values with names like "{yourOwnPrefix}:{varName}"
myPackage[yourOwnPrefix + ":MyCustomValue"] = "My simple data";
JpegOptions opt = new JpegOptions();
opt.XmpData = wrapper;
image.Save("test-add.jpg", opt);
}
Hope, it will help