如何使用C#向JPEG文件添加"注释"

Joh*_*n P 18 c# jpeg metadata

在JPEG图像的属性窗口中,有一个名为"摘要"的选项卡.在这个标签中,有一个名为"评论"的字段我想写一些c#代码,它会在这个字段中添加一个给定的字符串,例如"这是一张照片".

有些灵魂知道怎么做吗?

非常感谢.

Mar*_*den 16

基于其他答案,我编写了以下类,允许进行各种元数据操作.你这样使用它:

var jpeg = new JpegMetadataAdapter(pathToJpeg);
jpeg.Metadata.Comment = "Some comments";
jpeg.Metadata.Title = "A title";
jpeg.Save();              // Saves the jpeg in-place
jpeg.SaveAs(someNewPath);  // Saves with a new path
Run Code Online (Sandbox Code Playgroud)

我的解决方案与其他解决方案之间的差异并不大.主要是我重构了这个更清洁.我也使用更高级别的属性BitmapMetadata,而不是SetQuery方法.

以下是完整代码,根据MIT许可证授权.您需要将引用添加到PresentationCore,WindowsBaseSystem.Xaml.

public class JpegMetadataAdapter
{
    private readonly string path;
    private BitmapFrame frame;
    public readonly BitmapMetadata Metadata;

    public JpegMetadataAdapter(string path)
    {
        this.path = path;            
        frame = getBitmapFrame(path);
        Metadata = (BitmapMetadata)frame.Metadata.Clone();
    }

    public void Save()
    {
        SaveAs(path);
    }

    public void SaveAs(string path)
    {
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(frame, frame.Thumbnail, Metadata, frame.ColorContexts));
        using (Stream stream = File.Open(path, FileMode.Create, FileAccess.ReadWrite))
        {
            encoder.Save(stream);
        }
    }

    private BitmapFrame getBitmapFrame(string path)
    {
        BitmapDecoder decoder = null;
        using (Stream stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
        {
            decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        }
        return decoder.Frames[0];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • System.Xaml的引用也是必要的 (3认同)

Joh*_*n P 12

以下代码解决了我的问题,并为给定的JPEG图像添加了注释:

public void addImageComment(string imageFlePath, string comments)
    {
        string jpegDirectory = Path.GetDirectoryName(imageFlePath);
        string jpegFileName = Path.GetFileNameWithoutExtension(imageFlePath);

        BitmapDecoder decoder = null;
        BitmapFrame bitmapFrame = null;
        BitmapMetadata metadata = null;
        FileInfo originalImage = new FileInfo(imageFlePath);

        if (File.Exists(imageFlePath))
        {
            // load the jpg file with a JpegBitmapDecoder    
            using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            }

            bitmapFrame = decoder.Frames[0];
            metadata = (BitmapMetadata)bitmapFrame.Metadata;

            if (bitmapFrame != null)
            {
                BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();

                if (metaData != null)
                {
                    // modify the metadata   
                    metaData.SetQuery("/app1/ifd/exif:{uint=40092}", comments);

                    // get an encoder to create a new jpg file with the new metadata.      
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
                    //string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg");

                    // Delete the original
                    originalImage.Delete();

                    // Save the new image 
                    using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        encoder.Save(jpegStreamOut);
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这实际上是Konamiman友情提供的链接下的代码的轻微修改版本.

请注意,为了完成这项工作,您需要向PresentationCoreWindowsBase添加.NET引用.如果使用Visual Studio 2008,可以通过以下方式实现:

  1. 在解决方案资源管理器中右键单击您的项目

  2. 从下拉列表中,选择添加'参考...'

  3. 从打开的新框中,选择".NET"选项卡

  4. 滚动到上面提到的两个引用,然后单击确定

非常感谢danbystrom和Konamiman对此事的帮助.我非常感谢快速反应.

  • 鉴于jpeg是一种有损格式,您是否希望通过使用此方法添加元数据来降低质量,或者这只是修改元数据而不影响图片? (4认同)