将字节数组显示到文本文件 [英] Displaying byte array to a text file

查看:80
本文介绍了将字节数组显示到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像转换为字节数组并将其显示在文本文件中。



我将图像转换为字节数组...当我尝试把它显示在一个文本文件上,我不能这样做

....

 使用 System.Text; 
使用 System.Windows.Forms;
使用 System.IO;
使用 System.Drawing.Imaging;

namespace WindowsFormsApplication13
{
public partial class Form1:Form
{

public Form1()
{
InitializeComponent();
}

public byte [] ConvertImage(System.Drawing .Image sBit)
{
MemoryStream imageStream = new MemoryStream();
sBit.Save(imageStream,ImageFormat.Png);

return imageStream.ToArray();
}

public void button1_Click( object sender,EventArgs e)
{
Bitmap sBit = new 位图( @ C:\ Users \snehitha \ Pictures \ tongue\bin.bmp);
byte [] imageString = ConvertImage(sBit);

StreamWriter sw = new StreamWriter( @ D:\abc.txt false );
sw.Write(imageString);
sw.Close();
}
}
}



我的代码有什么问题?

我得到的输出是 system.byte [] ,我不明白为什么我得到这个结果......



请帮帮我。

解决方案

试试以下代码



< pre lang =c#> File.WriteAllBytes( @ D:\abc.txt, imageString);


您好,



而不是:

 StreamWriter sw =  new  StreamWriter( @  D:\abc.txt false ); 
sw.Write(imageString);
sw.Close();



尝试:

 File.WriteAllBytes(  D:\abc.txt,imageString); 









如果你想知道你的方法为什么写 System.Byte [] 到文本文件,这就是答案:

有没有 StreamWriter.Write(byte [])方法,但是因为那里'sa StreamWriter.Write(object)方法, StreamWriter 会将您的字节数组视为 object ,并将使用 ToString()方法获取对象的字符串表示。在这种情况下, imageString.ToString()返回 System.Byte []



希望这会有所帮助。


通常图像具有二进制文件格式,即存储为字节数组。如果要在文本文件中输出字节数组,通常需要以有意义的方式表示它,这是人类读者可能理解的方式。您可以使用最流行的格式之一来表示二进制数据,即十六进制 [ ^ ]一个。



按顺序完成你必须更换的任务

Quote:

sw.Write(imageString);



with

  foreach (b  in  imageString)
sw.write(b.ToString( X2)+ ); // 空白有助于人类读者...





这是非常基本的,您可能需要添加换行符和起始地址作为辅助信息。


I need to convert image to a byte array and display it on a text file.

I converted image to a byte array ...and when Im trying to display it on a text file, I couldn''t do it
....

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

       public byte[] ConvertImage(System.Drawing.Image sBit)
        {
            MemoryStream imageStream = new MemoryStream();
            sBit.Save(imageStream, ImageFormat.Png);

            return imageStream.ToArray();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            Bitmap sBit = new Bitmap(@"C:\Users\snehitha\Pictures\tongue\bin.bmp");
            byte[] imageString = ConvertImage(sBit);
                         
            StreamWriter sw = new StreamWriter(@"D:\abc.txt", false);
            sw.Write(imageString);
            sw.Close();
        }
    }
}


What is wrong in my code?
the output I get is system.byte[] ,I dont understad why I get this result...

Please help me.

解决方案

Try below code

File.WriteAllBytes(@"D:\abc.txt", imageString);


Hi,

Instead of:

StreamWriter sw = new StreamWriter(@"D:\abc.txt", false);
sw.Write(imageString);
sw.Close();


Try:

File.WriteAllBytes("D:\abc.txt", imageString);



[EDIT]

If you want to know why your method writes System.Byte[] to the text file, this is the answer:
there''s no StreamWriter.Write(byte[]) method, but because there''s a StreamWriter.Write(object) method, the StreamWriter will treat your byte array as an object, and will use the ToString() method to get the string representation of the object. And in this case, imageString.ToString() returns System.Byte[].

Hope this helps.


Usually an image has a binary file format, i.e. is stored as an array of bytes. If you want to output the array of bytes in a text file, you usually want to represent it in a meaningful way, that is in a way a human reader might understand. You could use one of the most popular format, for representing binary data, namely the hexadecimal[^] one.

In order to accomplish the task you have to replace

Quote:

sw.Write(imageString);


with

foreach( b in imageString)
  sw.write( b.ToString("X2") + " "); // the blank helps the human reader...



That is very basic, you probably need to add line breaks and starting addresses as auxiliary info.


这篇关于将字节数组显示到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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