Как я могу преобразовать строку в кодировке ASCII в BMP изображения
using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Media.Imaging; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public BitmapImage ToImage(byte[] array) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(array)) { BitmapImage image = new BitmapImage(); image.BeginInit(); image.StreamSource = ms; image.EndInit(); return image; } } public static byte[] ImageToBinary(string imagePath) { FileStream fS = new FileStream(imagePath, FileMode.Open, FileAccess.Read); byte[] b = new byte[fS.Length]; fS.Read(b, 0, (int)fS.Length); fS.Close(); return b; } public static string StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2).PadLeft(8, '0')); } return sb.ToString(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string result = System.Text.Encoding.ASCII.GetString(ImageToBinary(openFileDialog1.FileName)); System.IO.File.WriteAllText(@"C:\Users\Lenovo\Desktop\b.txt", result); } } public static Bitmap ByteToImage(byte[] blob) { MemoryStream mStream = new MemoryStream(); byte[] pData = blob; mStream.Write(pData, 0, Convert.ToInt32(pData.Length)); Bitmap bm = new Bitmap(mStream, false); mStream.Dispose(); return bm; } private void button2_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string input = File.ReadAllText(openFileDialog1.FileName); byte[] toBytes = Encoding.ASCII.GetBytes(input); // System.IO.File.WriteAllText(@"C:\Users\Lenovo\Desktop\a.bmp", ToImage(toBytes)); BitmapImage bmp1 = ToImage(toBytes); pictureBox1.Image = ByteToImage(toBytes); } } private void button3_Click(object sender, EventArgs e) { // SaveFileDialog sfd = new SaveFileDialog(); Bitmap varBmp = new Bitmap(pictureBox1.Image); Bitmap newBitmap = new Bitmap(varBmp); varBmp.Dispose(); varBmp = null; saveFileDialog1.Filter = "jpeg dosyası(*.jpg)|*.jpg|Bitmap(*.bmp)|*.bmp"; saveFileDialog1.Title = "Kayıt İşlemi"; saveFileDialog1.FileName = "resim"; DialogResult sonuç = saveFileDialog1.ShowDialog(); if (sonuç == DialogResult.OK) { newBitmap.Save(saveFileDialog1.FileName,ImageFormat.Bmp); } } } }
Что я уже пробовал:
Например предположим что у нас есть изображение ниже
Т — Postimage.org[^]
преобразуйте изображение в строку ascii и сохраните как txt.Я хочу сделать обратный процесс.Я читаю текст ascii и конвертирую его в исходное изображение, но оно еще не полностью сформировано.Подобный этому.Где моя ошибка
ресим — Postimage.org[^]