最近学习了图片的转换,就学习了一下图片从二进制转换成十六进制,十六进制TXT文件转换成图片形式。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace pictu{ public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //从文件目录中将图片读入到IO流中 FileStream fs = new FileStream(@"E:\图片转换测试\发送图片\1.jpg", FileMode.Open, FileAccess.Read); //对数据流进行编码八位二进制 BinaryReader br = new BinaryReader(fs); //形成十六进制最后存储的txt文件 StreamWriter sw = new StreamWriter(@"E:\图片转换测试\发送图片\1.txt"); //得到数据流的长度 int length = (int)fs.Length; while (length > 0) { byte tempByte = br.ReadByte(); //将八位二进制转换为十六进制字符串形式 string tempStr = Convert.ToString(tempByte, 16); //写入txt sw.WriteLine(tempStr); length--; } //关闭IO流释放资源 fs.Close(); br.Close(); sw.Close(); } ////// 原理与上面基本相同,仔细看就懂了 /// /// /// private void button2_Click(object sender, EventArgs e) { FileStream fs = new FileStream(@"E:\图片转换测试\接收图片\1.jpg", FileMode.Create, FileAccess.Write); //创建1.jpg图片 BinaryWriter bw = new BinaryWriter(fs); StreamReader sr = new StreamReader(@"E:\图片转换测试\发送图片\1.txt"); //打开你的16进制的txt while (sr.Peek() != -1) { string tempStr = sr.ReadLine(); byte tempByte = Convert.ToByte(tempStr, 16); bw.Write(tempByte); } fs.Close(); bw.Close(); sr.Close(); } private void Form1_Load(object sender, EventArgs e) { } }}