如何将图像转换为二进制并在文本框中获取结果 [英] How to convert image to binary and get result in text box

查看:68
本文介绍了如何将图像转换为二进制并在文本框中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像转换为二进制并在文本框中获得结果。



i使用以下函数:



图片在路径D中找到:/person.jpg



I need to convert image to binary and get result in text box .

i using the following function :

Image found in path D:/person.jpg

public static byte[] ImageToBinary(string _path)  
{  
FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);  
byte[] b = new byte[fS.Length];  
fS.Read(b, 0, (int)fS.Length);  
fS.Close();  
return b;  
}





如何在textbox1中接收函数ImageToBinary返回的值?



我在c#windows表格中工作c#



我尝试了什么:





How to receive the value returned from function ImageToBinary in textbox1 ?

I work in c# windows form c#

What I have tried:

How to convert image to binary and get result in text box

推荐答案

你拥有所有部分,你需要做的就是遍历字节数组并将每个数字转换为二进制表示。



请记住,通常有一个最大长度您可能需要手动克服的文本框(使其成为多行/增加最大长度等等。



You have all the parts, all you need to do is loop through the byte array and convert each number to binary representation.

Keep in mind there is usually a max length on a text box that you might have to manually overcome (make it multi-line / increase the max length, etc.

public static byte[] ImageToBinary(string _path)  
{  
    FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);  
    byte[] b = new byte[fS.Length];  
    fS.Read(b, 0, (int)fS.Length);  
    fS.Close();  
    return b;  
}

public static void SetText(string _path) {
    byte[] b = ImageToBinary(_path);
    
    StringBuilder binary = new StringBuilder();

    foreach (var bb in b) {
         binary.Append(Convert.ToString(bb, 2));
    }

    textBox1.Text = binary.ToString(); 

}


这篇关于如何将图像转换为二进制并在文本框中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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