bullsgl

博客园 首页 新随笔 联系 订阅 管理
 1 private void setImageTag(string imgFile, string tagFile, string imgTag, string imgTitle)
 2 {
 3     uint padding = 2048;
 4     if (File.Exists(imgFile))
 5     {
 6         BitmapDecoder decoder = null;
 7         // load the jpg file with a JpegBitmapDecoder  
 8         using (Stream jpegStreamIn = File.Open(imgFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
 9         {
10             decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
11         }
12         if (decoder != null)
13         {
14             BitmapFrame bitmapFrame = decoder.Frames[0];
15             if (bitmapFrame != null)
16             {
17                 // the size of the metadata has been increased and we can't save it  
18                 BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
19                 if (metaData != null)
20                 {
21                     // Add padding  
22                     metaData.SetQuery("/app1/ifd/PaddingSchema:Padding", padding);
23                     metaData.Title = imgTitle;
24                     List<string> taglist = new List<string>();
25                     taglist.Add(imgTag);
26                     metaData.Keywords = new System.Collections.ObjectModel.ReadOnlyCollection<string>(taglist);
27                     // get an encoder to create a new jpg file with the addit'l metadata.  
28                     JpegBitmapEncoder encoder = new JpegBitmapEncoder();
29                     encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
30                     using (Stream jpegStreamOut = File.Open(tagFile, FileMode.CreateNew, FileAccess.ReadWrite))
31                     {
32                         encoder.Save(jpegStreamOut);
33                     }
34                 }
35             }
36         }
37     }
38 }

其中22行,添加PADDING是不能少的,否则编辑信息保存不上。

posted on 2013-12-16 16:53  bullsgl  阅读(1572)  评论(0)    收藏  举报