在不丢失EXIF数据的情况下,用.NET调整图像的大小

时间:2021-07-21 21:49:30

What is the best way to resize images using .NET, without losing the EXIF data? I'm okay with using .NET 2 System.Drawing.* classes, WPF classes, or open-source libraries.

在不丢失EXIF数据的情况下,使用.NET调整图像大小的最佳方法是什么?我不介意使用。net 2系统。*类、WPF类或开源库。

The only easy way I found to handle this all for now is to use the Graphics.FromImage (.NET 2) to perform the resizing and to re-write the EXIF data with an OpenSource library manually (each piece of data one by one).

我现在找到的唯一简单的方法就是使用图形。FromImage(。NET 2)手动执行调整大小并使用一个OpenSource库重新编写EXIF数据(每个数据块一个地)。

2 个解决方案

#1


3  

Your suggestion of extracting the EXIF data before resizing, and then re-inserting the EXIF data seems like a decent solution.

您建议在调整大小之前提取EXIF数据,然后重新插入EXIF数据,这似乎是一个不错的解决方案。

EXIF data can be defined only for formats like JPEG and TIFF - when you load such an image into a Graphics object for resizing, you're essentially converting the image to a regular bitmap. Hence, you lose the EXIF data.

EXIF数据只能为JPEG和TIFF格式定义—当您将这样的图像加载到一个图形对象中进行调整大小时,实际上是将图像转换为一个普通的位图。因此,您将丢失EXIF数据。

Slightly related thread about EXIF extraction using C# here.

这里使用c#对EXIF的提取稍微相关一些。

#2


2  

I used Magick .NET and created 2 extension methods:

我使用Magick。net创建了两个扩展方法:

    public static byte[] ToByteArray(this Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

    public static Image AttachMetadData(this Image imgModified, Image imgOriginal)
    {
        using (MagickImage imgMeta = new MagickImage(imgOriginal.ToByteArray()))
        using (MagickImage imgModi = new MagickImage(imgModified.ToByteArray()))
        {
            foreach (var profileName in imgMeta.ProfileNames)
            {
                imgModi.AddProfile(imgMeta.GetProfile(profileName));
            }
            imgModified = imgModi.ToImage();
        }
        return imgModified;
    }

#1


3  

Your suggestion of extracting the EXIF data before resizing, and then re-inserting the EXIF data seems like a decent solution.

您建议在调整大小之前提取EXIF数据,然后重新插入EXIF数据,这似乎是一个不错的解决方案。

EXIF data can be defined only for formats like JPEG and TIFF - when you load such an image into a Graphics object for resizing, you're essentially converting the image to a regular bitmap. Hence, you lose the EXIF data.

EXIF数据只能为JPEG和TIFF格式定义—当您将这样的图像加载到一个图形对象中进行调整大小时,实际上是将图像转换为一个普通的位图。因此,您将丢失EXIF数据。

Slightly related thread about EXIF extraction using C# here.

这里使用c#对EXIF的提取稍微相关一些。

#2


2  

I used Magick .NET and created 2 extension methods:

我使用Magick。net创建了两个扩展方法:

    public static byte[] ToByteArray(this Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

    public static Image AttachMetadData(this Image imgModified, Image imgOriginal)
    {
        using (MagickImage imgMeta = new MagickImage(imgOriginal.ToByteArray()))
        using (MagickImage imgModi = new MagickImage(imgModified.ToByteArray()))
        {
            foreach (var profileName in imgMeta.ProfileNames)
            {
                imgModi.AddProfile(imgMeta.GetProfile(profileName));
            }
            imgModified = imgModi.ToImage();
        }
        return imgModified;
    }