在文件中写入元标签

本文关键字:标签 文件 | 更新日期: 2023-09-27 17:53:47

当我试图通过链接https://drive.google.com/file/d/0BzSOSmEHZLIjNkFiSE5qQmFEdlE/view?usp=sharing在文件中编写元标签时使用EditTags()方法我得到了异常(图像数据在处理过程中产生溢出)。但是当我尝试写元标签时,对于其他jpg文件都是正确的。

public void EditTags()
    {
        string imageFlePath = "d:''Vatche-Swan-Solitaire-Engagement-and-Wedding-Rings-in-18k-White-Gold-from-Whiteflash_41313_18301_f.jpg";
        BitmapDecoder decoder = null;
        BitmapFrame bitmapFrame = null;
        BitmapMetadata metadata = null;
        FileInfo originalImage = new FileInfo(imageFlePath);
        if (System.IO.File.Exists(imageFlePath))
        {
            using (Stream jpegStreamIn = System.IO.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;
            Collection<System.Windows.Media.ColorContext> gdf = new Collection<System.Windows.Media.ColorContext>();
            ReadOnlyCollection<System.Windows.Media.ColorContext> dgfd = new ReadOnlyCollection<System.Windows.Media.ColorContext>(gdf);
            if (bitmapFrame != null)
            {
                BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
                if (metaData != null)
                {
                    metaData.SetQuery("System.Keywords", "Test");
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
                    originalImage.Delete();
                    using (Stream jpegStreamOut = System.IO.File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        encoder.Save(jpegStreamOut);
                    }
                }
            }
        }
    }

在文件中写入元标签

我找到了解决方案。"/xmpMM:History"列表中的所有问题。当我不保存这个标签时,一切都是正确的。但是我怎么检查其他标签呢?

static void Main(string[] args)
    {
        string imageFlePath = "d:''1.jpg";
        BitmapDecoder decoder = null;
        BitmapFrame bitmapFrame = null;
        FileInfo originalImage = new FileInfo(imageFlePath);
        if (System.IO.File.Exists(imageFlePath))
        {
            using (Stream jpegStreamIn = System.IO.File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            }
            bitmapFrame = decoder.Frames[0];
            Collection<System.Windows.Media.ColorContext> gdf = new Collection<System.Windows.Media.ColorContext>();
            ReadOnlyCollection<System.Windows.Media.ColorContext> dgfd = new ReadOnlyCollection<System.Windows.Media.ColorContext>(gdf);
            if (bitmapFrame != null)
            {
                BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
                BitmapMetadata mData2 = new BitmapMetadata("jpg");
                foreach (string key in metaData)
                {
                    if (key == "/xmp")
                    {
                        BitmapMetadata subSrc = (BitmapMetadata)metaData.GetQuery(key);
                        BitmapMetadata subDest = new BitmapMetadata("xmp");
                        foreach (string subKey in subSrc)
                        {
                            if (subKey != "/xmpMM:History")
                            {
                                var val = subSrc.GetQuery(subKey);
                            }
                        }
                        mData2.SetQuery(key, subDest);
                    }
                    else
                    {
                        mData2.SetQuery(key, metaData.GetQuery(key));
                    }
                }
                if (metaData != null)
                {
                    metaData.SetQuery("System.Keywords", "Test");
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, mData2, bitmapFrame.ColorContexts));
                    //originalImage.Delete();
                    using (Stream jpegStreamOut = System.IO.File.Open("d:''2.jpg", FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        encoder.Save(jpegStreamOut);
                    }
                }
            }
        }
    }