当我尝试通过链接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);
}
}
}
}
}
答案 0 :(得分:0)
我找到了解决方案。 “/ 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);
}
}
}
}
}