这是可能的。实际上,您需要将000001111100000111111100000011
或000111000111000111000111000111
转换为string
作为字节数组byte[]
。要做到这一点,我们将使用Convert.ToByte(object value, IFormatProvider provider)
string input = "BINARY GOES HERE";
int numOfBytes = input.Length/8; //Get binary length and divide it by 8
byte[] bytes = new byte[numOfBytes]; //Limit the array
for (int i = 0; i < numOfBytes; ++i)
{
bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); //Get every EIGHT numbers and convert to a specific byte index. This is how binary is converted :)
}
这将宣布一个新的字符串input
,然后将其编码为一个字节数组(byte[]
)。我们实际上需要这个byte[]
作为Stream
,然后将其插入我们的PictureBox
。要做到这一点,我们将使用MemoryStream
MemoryStream FromBytes = new MemoryStream(bytes); //Create a new stream with a buffer (bytes)
最后,我们可以简单地使用该流来创建我们Image
文件和文件设置为我们PictureBox
pictureBox1.Image = Image.FromStream(FromBytes); //Set the image of pictureBox1 from our stream
例
private Image Decode(string binary)
{
string input = binary;
int numOfBytes = input.Length/8;
byte[] bytes = new byte[numOfBytes];
for (int i = 0; i < numOfBytes; ++i)
{
bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2);
}
MemoryStream FromBinary = new MemoryStream(bytes);
return Image.FromStream(FromBinary);
}
通过使用这个,你可以随时打电话,例如Decode(000001111100000111111100000011)
,它会产生rt它为您的图像。然后,您可以使用此代码来设置PictureBox
pictureBox1.Image = Decode("000001111100000111111100000011");
重要通告的Image
属性:您可能会收到ArgumentException was unhandled: Parameter is not valid.
这可能发生因无效的二进制代码。
谢谢,
我希望对您有所帮助:)
myTextbox.Text?我需要在你的问题中看到一些代码! – LightStriker
听起来像你最好开始写一些代码......如果这是一个学校作业,如果你没有理解基本的编码,你会不会对你有任何好处。如果教师问你你怎么办来了你的答案/解决方案..?那么你会在头灯看起来像一只鹿..! – MethodMan
您已经描述了一个项目,但没有提出问题,您是否在阅读文本框中的文本,在图片框中绘制文本,绘制线条,计算结果,绘制结果等方面遇到问题。哪一部分困扰你? – Casperah