1private void setImageTag(string imgFile, string tagFile, string imgTag, string imgTitle)
2 {
3uint padding = 2048;
4if (File.Exists(imgFile))
5 {
6 BitmapDecoder decoder = null;
7// load the jpg file with a JpegBitmapDecoder
8using (Stream jpegStreamIn = File.Open(imgFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
9 {
10 decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
11 }
exif信息12if (decoder != null)
13 {
14 BitmapFrame bitmapFrame = decoder.Frames[0];
15if (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();
19if (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)); 30using (Stream jpegStreamOut = File.Open(tagFile, FileMode.CreateNew, FileAccess.ReadWrite))
31 {
32 encoder.Save(jpegStreamOut);
33 }
34 }
35 }
36 }
37 }
38 }
发布评论