zoukankan      html  css  js  c++  java
  • 在图片内写入内容

     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;
            }
        }
  • 相关阅读:
    centos7安装mysql5.7
    centos7 多网卡绑定bond0 之mod4
    二、Windows Server 2016 AD 组织单位、组、用户的创建
    一、Windows Server 2016 AD服务器搭建
    (2)Linux中经常说的CPU上下文切换是什么意思
    (1)Linux系统中到底应该怎么理解系统的平均负载
    centos安装Docker与使用&&构建业务镜像挂载卷harbor仓库的高可用及网络模式和资源限制介绍
    windows和linux各类服务常用端口号汇总
    CentOS7.6搭建LAMP-wordpress论坛搭建
    centos下iptables防火墙规则用法和概述
  • 原文地址:https://www.cnblogs.com/yuanchao/p/11165445.html
Copyright © 2011-2022 走看看