0

I'm trying to manipulate the meta data of an Image, so i can compress and decompress it without losing information, I can already read the image meta data, but I can't actually edit them.

I found this similar question: How to set extended file properties? But it doesn't seem to work today, as it is pretty old.

Edit: The project is for IOS and Android.

  • Not sure if this helps but according to [the Microsoft docs](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-read-image-metadata?view=netframeworkdesktop-4.8) GDI+ should allow for both reading existing metadata and writing new metadata to image files. – Sven Viking Apr 14 '22 at 14:59
  • Hey thanks for this info. I'm struggling to make it compile, it seems like the namespaces that this script needs is "System.Drawing.Imaging", and "System.Drawing.Bitmap", but my script don't seem to recognize them? Am I missing anything? Unity just gives me an error "[Script path] error CS0234: The type or namespace name [Bitmap or Imaging] does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)". – Rafael Gomes Apr 14 '22 at 16:51
  • You may need to add the `System.Drawing.Common` NuGet package to your project, as that's where types such as `Bitmap` are defined (`Bitmap` is part of the Windows-only .net full framework, but when using .net core it's one of the things which has been moved out into a separate package) – sbridewell Apr 14 '22 at 18:27
  • 1
    Hey, thanks for your answer, installing the NuGet package made it found the namespaces, but it was not compatible with unity and mobile. – Rafael Gomes Apr 14 '22 at 19:11

1 Answers1

0

I managed to use SharpZipLib in unity: https://www.nuget.org/packages/SharpZipLib/ and compress the image to a zip:

using System.IO.Compression;

private string CompressImage(string picturePersistentPath)
{
    string pictureCompressedPath = Path.Combine(Application.persistentDataPath, "Compressed.zip");
        
    using FileStream originalFileStream = File.Open(picturePersistentPath, FileMode.Open);
    using FileStream compressedFileStream = File.Create(pictureCompressedPath);
    using var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress);
    originalFileStream.CopyTo(compressor);

    return pictureCompressedPath;
}