本文介绍了如何将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);
}
}
}
}
我尝试了什么:
例如假设我们有一张图片
[]
将图像转换为ascii字符串并保存为txt。我想做反向process.I读取ascii文本并转换为原始图像,但它没有完全形成。就像这样。我的错误是什么
[]
推荐答案
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图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!