求教,怎么将txt文件中的十六进制码还原成图片

xiaozhuer000 2017-11-14 05:14:42
一张图片转换成十六进制码远程发给我,我接收过后存到txt中,现在想通过C#编写代码,让这十六进制码还原成图片,请问怎么弄,必当重谢,新手一枚,不太懂C#。谢谢了,用C#窗体写
...全文
597 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 13 楼 xiaozhuer000 的回复:
引用 11 楼 xomix 的回复:
[quote=引用 10 楼 xiaozhuer000 的回复:] [quote=引用 1 楼 xiaozhuer000 的回复:] 求各位大神相助,网上全是用java或者C++写的
是把我txt中的十六进制码变成图片,这个确实不太会
txt中 是类似 ff f0 e3 这样的集合吗? 那你就少了一个string转byte罢了,百度一下即可。[/quote] 意思是直接将txt中的FF 2A这种十六进制码直接放入到byte中吗? [/quote] 是的,百度一下有很多。
xiaozhuer000 2017-11-21
  • 打赏
  • 举报
回复
引用 11 楼 xomix 的回复:
引用 10 楼 xiaozhuer000 的回复:
[quote=引用 1 楼 xiaozhuer000 的回复:] 求各位大神相助,网上全是用java或者C++写的
是把我txt中的十六进制码变成图片,这个确实不太会
txt中 是类似 ff f0 e3 这样的集合吗? 那你就少了一个string转byte罢了,百度一下即可。[/quote] 意思是直接将txt中的FF 2A这种十六进制码直接放入到byte中吗?
xiaozhuer000 2017-11-21
  • 打赏
  • 举报
回复
引用 3 楼 xiaoruanzhu 的回复:

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // 将照片转换成 16进制字符串:  
  
            string strData = "";  
            using (FileStream fsReade = new FileStream("00.jpg", FileMode.OpenOrCreate, FileAccess.Read))  
            {  
                // 声明一个byte数组用来读取照片:  
                byte[] buf = new byte[60000];  
                int n = 0;  
                int len = 0;  
  
                // 循环读取照片,并存入byte数组中:  
                while (true)  
                {  
                    len = fsReade.Read(buf, n, 1024);  
                    if (len == 0)  
                    {  
                        break;  
                    }  
                    n = n + len;  
                }  
  
                for (int i = 0; i < n; i++)  
                {  
                    // 将byte数组中的每一个字节都转换成16进制字符串:  
                    strData += buf[i].ToString("X2");  
                }  
            }  
  
            // 将16进制字符串转换成照片:  
  
            using (FileStream fsWrite = new FileStream("11.jpg", FileMode.OpenOrCreate, FileAccess.Write))  
            {  
                // 声明一个字节数组,长度为16进制字符串长度的一半:  
                byte[] buf = new byte[strData.Length / 2];  
  
                for (int i = 0; i < buf.Length; i++)  
                {  
                    // 由于16进制字符串都是两个一组,所以需要将两个字符一起转换成字节  
                    buf[i] = Convert.ToByte(strData.Substring(i * 2, 2), 16);  
                }  
  
                // 将字节数组写入照片中:  
                fsWrite.Write(buf, 0, buf.Length);  
            }  
        }  
    }  
}  
这个是16进制与图片转换的
请问这个怎么在窗体中写,不是很懂
xiaozhuer000 2017-11-21
  • 打赏
  • 举报
回复
引用 1 楼 xiaozhuer000 的回复:
求各位大神相助,网上全是用java或者C++写的
是把我txt中的十六进制码变成图片,这个确实不太会
  • 打赏
  • 举报
回复
引用 10 楼 xiaozhuer000 的回复:
引用 1 楼 xiaozhuer000 的回复:
求各位大神相助,网上全是用java或者C++写的
是把我txt中的十六进制码变成图片,这个确实不太会
txt中 是类似 ff f0 e3 这样的集合吗? 那你就少了一个string转byte罢了,百度一下即可。
帅猪儿 2017-11-16
  • 打赏
  • 举报
回复
收藏了!哈哈,回答晚了。
凤凰居士 2017-11-16
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
wipeout 2017-11-16
  • 打赏
  • 举报
回复
学习了 用的是文件流
smileruner 2017-11-15
  • 打赏
  • 举报
回复
看来是需要使用到流的。 学习了。
楚楚3107 2017-11-15
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
看看2楼的。思路是:1.先读取文件到数组。2.反序列化。3.使用反序列化对象生成Image或Bitmap 图像。
xuzuning 2017-11-14
  • 打赏
  • 举报
回复
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
xiaoruanzhu 2017-11-14
  • 打赏
  • 举报
回复

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // 将照片转换成 16进制字符串:  
  
            string strData = "";  
            using (FileStream fsReade = new FileStream("00.jpg", FileMode.OpenOrCreate, FileAccess.Read))  
            {  
                // 声明一个byte数组用来读取照片:  
                byte[] buf = new byte[60000];  
                int n = 0;  
                int len = 0;  
  
                // 循环读取照片,并存入byte数组中:  
                while (true)  
                {  
                    len = fsReade.Read(buf, n, 1024);  
                    if (len == 0)  
                    {  
                        break;  
                    }  
                    n = n + len;  
                }  
  
                for (int i = 0; i < n; i++)  
                {  
                    // 将byte数组中的每一个字节都转换成16进制字符串:  
                    strData += buf[i].ToString("X2");  
                }  
            }  
  
            // 将16进制字符串转换成照片:  
  
            using (FileStream fsWrite = new FileStream("11.jpg", FileMode.OpenOrCreate, FileAccess.Write))  
            {  
                // 声明一个字节数组,长度为16进制字符串长度的一半:  
                byte[] buf = new byte[strData.Length / 2];  
  
                for (int i = 0; i < buf.Length; i++)  
                {  
                    // 由于16进制字符串都是两个一组,所以需要将两个字符一起转换成字节  
                    buf[i] = Convert.ToByte(strData.Substring(i * 2, 2), 16);  
                }  
  
                // 将字节数组写入照片中:  
                fsWrite.Write(buf, 0, buf.Length);  
            }  
        }  
    }  
}  
这个是16进制与图片转换的
xiaoruanzhu 2017-11-14
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
        {
            FileStream readFs = new FileStream("C:/Users/20160325/Desktop/QQ图片20171114181558.png", FileMode.Open, FileAccess.ReadWrite);
            byte[] textureByte=new byte[readFs.Length];
            
            readFs.Read(textureByte, 0,(int) readFs.Length);
            MemoryStream buf =new MemoryStream(textureByte);
            Image image=Image.FromStream(buf,true);
            //保存图片
            pictureBox1.Image = image;
        }
经测试可用,希望能帮到你
xiaozhuer000 2017-11-14
  • 打赏
  • 举报
回复
求各位大神相助,网上全是用java或者C++写的

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧