Aspose.Imaging | On saving image metadata gets disapperead

Hi Team,

I am using Aspose.Imaging for working with XMP metadata. I am setting the metadata which works fine. And then I am calling image.Save(filePath) to save the file. But, after this when in the next call I try to call image.Load(filePath), that instance does not have any xmpData.
However, when I try to comment out the code line → image.Save(filePath)
It works! Atleast in the next call I can see the old metadata. But when I keep that Save function, the whole metadata, old as well as updated, is gone! :frowning:
Please help me out.

@DeepakSeclore

It seems that you are experiencing an issue where the XMP metadata is lost after saving the image using Aspose.Imaging. This can happen if the image saving options do not preserve the metadata correctly.

To ensure that the XMP metadata is retained when saving the image, you should use the setKeepMetadata(true) method in your image options before saving. This option allows the original image metadata to be preserved during the export process.

Here’s a sample code snippet demonstrating how to set this up:

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Xmp;
using System;

class Program
{
    static void Main()
    {
        string filePath = "path_to_your_image.dcm";
        
        // Load the DICOM image
        using (DicomImage image = (DicomImage)Image.Load(filePath))
        {
            // Create an XMP packet wrapper and add your metadata
            XmpPacketWrapper xmpPacketWrapper = new XmpPacketWrapper();
            // Add your metadata to xmpPacketWrapper here

            // Set the XMP data in the DICOM options
            DicomOptions dicomOptions = new DicomOptions
            {
                XmpData = xmpPacketWrapper,
                KeepMetadata = true // Ensure metadata is kept
            };

            // Save the image with the options
            image.Save("output.dcm", dicomOptions);
        }

        // Load the saved image to verify metadata
        using (DicomImage savedImage = (DicomImage)Image.Load("output.dcm"))
        {
            // Check if the XMP data is present
            var xmpData = savedImage.XmpData;
            if (xmpData != null)
            {
                Console.WriteLine("XMP metadata is preserved.");
            }
            else
            {
                Console.WriteLine("XMP metadata is missing.");
            }
        }
    }
}

In this example, make sure to replace "path_to_your_image.dcm" with the actual path to your DICOM image. The KeepMetadata property is crucial for ensuring that the metadata is not lost during the save operation.

If you continue to face issues, please check if there are any specific limitations or requirements for the image format you are working with, as some formats may have different behaviors regarding metadata handling.

Thanks a lot for the prompt response! And I think you provided me the correct solution. But there is another issue that I am facing now. When I added PngOptions and tried to call Save function, it threw an error saying -
Data - System.Collections.ListDictionaryInternal
Message - Image saving failed.
InnerException - The process cannot access file “C:\…” because it is being used by another process.

Can you please help me to resolve this?

Hello @DeepakSeclore
Perhaps, you are trying to save the image in the same file. Am I right?
If you wish to save in the same file you should use Image.Save() method without any params.

Hello @DeepakSeclore ,
Could you share the code example, the src image and the product version, so we can investigate the issue and give a feedback.
Our apologies for the inconvenience you are facing!

I am using 24.12 version. And I have tried using image.Save() method as well. But the issue still persists. Unfortunately, I can’t share the code as it is offline env. Can you help me without that?

@DeepakSeclore , we are reviewing your request. You will be answered shortly!

I am not able to read XMP metadata for PNG image files. Is PNG format not supported for reading XMP?

Hi @DeepakSeclore
Png, Jpeg, Tiff, Gif, Psd, Svg, Jpeg2000 formats currently fully support Xmp load/save. Here is a code example to check if src image has Xmp metadata and checks the saved image one:

public void ShowXmpData(string inputPath)
{
    var outputs = new List<string>
    {
        inputPath + Path.GetExtension(inputPath),
        inputPath + "-KeepMetadat.png",
        inputPath + "-XmpData.png",
    };

    string xmp = null;
    XmpPacketWrapper xmpData = null;
    using (var image = Image.Load(inputPath))
    {
        if (image is IHasXmpData hasXmpData)
        {
            xmp = hasXmpData.XmpData?.ToString();
            xmpData = hasXmpData.XmpData;
        }

        try
        {
            // saves original Xmp when specified stream or path has the same extension as src.
            image.Save(outputs[0]);
        }
        catch (Exception e)
        {
            // check if current Image supports save.
            Console.WriteLine(e);
        }

        // tries keep original image metadata on save.
        image.Save(outputs[1], new PngOptions
        {
            KeepMetadata = true,
        });

        // excplitly sets Xmp data for the saved image.
        image.Save(outputs[2], new PngOptions
        {
            XmpData = xmpData,
        });
    }

    Console.WriteLine($"Source Xmp: {xmp}\n");

    foreach (var outputPath in outputs)
    {
        string exportedXmp = null;
        using (var image = Image.Load(outputPath))
        {
            if (image is IHasXmpData hasXmpData)
            {
                exportedXmp = hasXmpData.XmpData?.ToString();
            }
        }

        Console.WriteLine(outputPath);
        Console.WriteLine($"Exported Xmp: {exportedXmp}\n");
    }
}

We have also opened the following new ticket(s) in our internal issue tracking system to clarify Image metadata API so user can check if a file actually supports Xmp load/save. We will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): IMAGINGNET-7589

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@DeepakSeclore ,
I have also forget to mention, please you try using the latest Aspose.Imaging version which is 25.03.