如何将ascii字符串转换为bmp图像 [英] How do I convert ascii string to bmp image

查看:116
本文介绍了如何将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);
            }
        }
    }
}





我尝试了什么:



例如假设我们有一张图片



t - Postimage.org [ ^ ]





将图像转换为ascii字符串并保存为txt。我想做反向process.I读取ascii文本并转换为原始图像,但它没有完全形成。就像这样。我的错误是什么



resim - Postimage.org [ ^ ]

推荐答案

您的错误是在转换过程中。 ASCII.ToString不会将二进制数据转换为文本 - 它会创建一个包含二进制数据的字符串。为此,您需要一种编码算法,如UUEncode或Base64。



Convert类具有简单的Base64转换方法。所以一旦从图像中获得了字节数组,就执行

Your error is in the conversion process. ASCII.ToString does not convert binary data into text - it creates a string that contains binary data. For that, you need an encoding algorithm such as UUEncode or Base64.

The Convert class has simple Base64 conversion methods. So once you have your byte array from the image, perform
string text = Convert.ToBase64String(data)



和其他方式


and to go the other way

byte[] data = Convert.FromBase64String(text)





然后你可以通过打开一个ASCII编码来读/写文件带有Encoding.ASCII参数的StreamReader或StreamWriter。



You can then read/write the file with ASCII encoding by opening a StreamReader or StreamWriter with the Encoding.ASCII parameter.


这篇关于如何将ascii字符串转换为bmp图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆