弹来弹去跑马灯!

图片写入额外文本信息

有的游戏载入一个存档图片就能还原当时的数据,应该是利用图片写入有纯当的额外数据。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        string pathPic = @"D:\1.jpg";
        private void button1_Click(object sender, EventArgs e)
        {
           
         // addImageComment(path1, "test wgscd 测试!!");
            addImageComment(pathPic, Properties.Resources.String1);
        }
        private void btn2_Click(object sender, EventArgs e)
        { 
            string msg = extractComment(pathPic);
            MessageBox.Show(msg);
        }

        /// <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;
        }


    }


}

 

 

 另外:

C# using System.Windows.Media.Imaging;该引用哪个dll
在网上看到BitmapSource和WriteableBitmap一些类听说是用 using System.Windows.Media.Imaging;可是我发现VS中没有什么System.Windows.Media.Imaging之类的框架,这就苦逼了,在网上也找不到,无意间发现原来引用不叫System.Windows.Media.Imaging,而是

PresentationCore

只需要在引用-->程序集-->框架-->PresentationCore找到这个就行了,另外我发现using System.Windows.Media不仅仅是在PresentationCore中有,而且WindowsBase也有引用方式

引用-->程序集-->框架-->WindowsBase

以后大家就不要再走弯路了,这个引用比较邪门,居然using System.Windows.Media.Imaging要引用PresentationCore真是不可思议!

 

posted @ 2020-01-20 10:02  wgscd  阅读(619)  评论(3)    收藏  举报