private void setImageTag(string imgFile, string tagFile, string imgTag, string imgTitle)
{
uint padding = ;
if (File.Exists(imgFile))
{
BitmapDecoder decoder = null;
// load the jpg file with a JpegBitmapDecoder
using (Stream jpegStreamIn = File.Open(imgFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
if (decoder != null)
{
BitmapFrame bitmapFrame = decoder.Frames[];
if (bitmapFrame != null)
{
// the size of the metadata has been increased and we can't save it
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
if (metaData != null)
{
// Add padding
metaData.SetQuery("/app1/ifd/PaddingSchema:Padding", padding);
metaData.Title = imgTitle;
List<string> taglist = new List<string>();
taglist.Add(imgTag);
metaData.Keywords = new System.Collections.ObjectModel.ReadOnlyCollection<string>(taglist);
// get an encoder to create a new jpg file with the addit'l metadata.
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
using (Stream jpegStreamOut = File.Open(tagFile, FileMode.CreateNew, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
}
}
}
}
其中22行,添加PADDING是不能少的,否则编辑信息保存不上。