C#——HEX字符串转jpg

顺便记录一下调试过程

  returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

    报错找不到任何可识别的数字

    原因是我的HEX字符串中存在空格,去掉就好了

  System.Drawing.Image img = System.Drawing.Image.FromStream((Stream)ms); //把内存的流转换成图片格式
  img.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);// 保存图片

    这两句会报参数无效的错误,实际上是读写数据流超时,网上找了好多代码都会报这个错误,不知道咋回事这篇代码就不报

读写的文件都存在bin/Debug中

using System;
using System.Text;
using System.IO;


namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string txt = Encoding.Default.GetString(ReadFile("16string.txt"));
            string fmtxt = "";
            string[] strs = txt.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < strs.Length - 1; i++)
            {
                if (strs[i].Trim().Length == 1)
                {
                    fmtxt += "0" + strs[i].Trim().ToUpper();
                }
                else
                {
                    fmtxt += strs[i].Trim().ToUpper();
                }
            }
            fmtxt = fmtxt.Replace(" ","");
            byte[] bt = HexToByte(fmtxt);
            SaveImage("picture.jpg", bt);
        }
        /// <summary>
        /// 读取文件流 返回 byte[]
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        static public byte[] ReadFile(string filePath)
        {
            byte[] buffer;
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {

                int length = (int)fileStream.Length; // 获取文件流长度
                buffer = new byte[length]; // 创建byte[]数组
                int count; // 实际读取的字节数
                int sum = 0; // 读取的字节总数

                //读取流已经到达的位置,直到方法返回0时结束
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count; // 下一个读取流起始位置

            }
            finally
            {
                fileStream.Close();
            }
            return buffer;
        }
        /// <summary>
        /// 转换为16进制 返回 byte[]
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        static private byte[] HexToByte(string hexString)
        {
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                //returnBytes[i] = Convert.ToByte(hexString.Substring(i * 1, 1), 16);
            return returnBytes;
        }

        /// <summary>
        /// 保存图片 JPEG格式
        /// </summary>
        /// <param name="fileName">保存图片的路径 JPEG格式</param>
        /// <param name="bt"></param>
        static private void SaveImage(string fileName, byte[] bt)
        {
            MemoryStream ms = new MemoryStream(bt); //建立内存的流
            System.Drawing.Image img = System.Drawing.Image.FromStream((Stream)ms); //把内存的流转换成图片格式
            img.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);// 保存图片
        }

        /// <summary>
        /// 写入文件流并保存
        /// </summary>
        /// <param name="FileName">保存文件的路径</param>
        /// <param name="Content">内容</param>
        protected void Write_Txt(string FileName, string Content)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            string htmlfilename = FileName; //保存文件的路径
            string str = Content;
            StreamWriter sw = null;
            {
                try
                {
                    sw = new StreamWriter(htmlfilename, false, code);
                    sw.Write(str);
                    sw.Flush();
                }
                catch { }
            }
            sw.Close();
            sw.Dispose();
        }
    }
}

 

上一篇:MaixPy K210 目标分类模型在线训练及使用教程


下一篇:如何快速压缩图片