在图片内写入内容

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string path1 = @"D:\3-153623-C1.jpeg";
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            addImageComment(path1, "胡AAA,SDD2212胡2234火狐");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string msg = extractComment(path1);
        }

/// <summary>
        /// 字符串转Unicode码
        /// </summary>
        /// <returns>The to unicode.</returns>
        /// <param name="value">Value.</param>
        private string StringToUnicode(string value)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(value);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                // 取两个字符,每个字符都是右对齐。
                stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// Unicode转字符串
        /// </summary>
        /// <returns>The to string.</returns>
        /// <param name="unicode">Unicode.</param>
        private string UnicodeToString(string unicode)
        {
            string resultStr = "";
            string[] strList = unicode.Split('u');
 for (int i = 1; i < strList.Length; i++)
            {
                resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
            }
            return resultStr;
        }
 public void addImageComment(string imageFlePath, string comments)
        {
            string jpegDirectory = System.IO.Path.GetDirectoryName(imageFlePath);
            string jpegFileName = System.IO.Path.GetFileNameWithoutExtension(imageFlePath);
            BitmapDecoder decoder = null;
            BitmapFrame bitmapFrame = null;
            BitmapMetadata metadata = null;
            if (System.IO.File.Exists(imageFlePath))
            {
                // load the jpg file with a JpegBitmapDecoder    
                using (Stream jpegStreamIn = 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;
 if (bitmapFrame != null)
                {
                    BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
                    if (metaData != null)
                    {
                        string ascStr = StringToUnicode(comments);
                        // modify the metadata   
                        metaData.SetQuery("/app1/ifd/exif:{uint=37510}", ascStr);//comment
                        // get an encoder to create a new jpg file with the new metadata.      
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
                        // Save the new image 
                        using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.Create, FileAccess.ReadWrite))
                        {
                            encoder.Save(jpegStreamOut);
                    }
                    }
                }
            }
        }
 public string extractComment(string imageFilePath)
        {
            string getinfo = "";
            string jpegDirectory = System.IO.Path.GetDirectoryName(imageFilePath);
            string jpegFileName = System.IO.Path.GetFileNameWithoutExtension(imageFilePath);
            BitmapDecoder decoder = null;
            BitmapFrame bitmapFrame = null;
            BitmapMetadata metadata = null;
            FileInfo originalImage = new FileInfo(imageFilePath);
            if (File.Exists(imageFilePath))
 {
                // load the jpg file with a JpegBitmapDecoder    
                using (Stream jpegStreamIn = File.Open(imageFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                }
                bitmapFrame = decoder.Frames[0];
                metadata = (BitmapMetadata)bitmapFrame.Metadata;
                if (bitmapFrame != null)
                {
                    BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
                    if (metaData != null)
{
                        try
                        {
                            byte[] info = Encoding.Default.GetBytes(metadata.GetQuery("/app1/ifd/exif:{uint=37510}").ToString());
                            getinfo = Encoding.ASCII.GetString(info);
                        }
                        catch 
                        {
                            return string.Empty;
                        }
                    }
  }
            }
            string ret = UnicodeToString(getinfo);
            return ret;
        }
    }

 

posted on 2019-07-10 17:44  yuanchaost 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/yuanchao/p/11165445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值