这个有可能。您实际上需要转换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[]
)。我们实际上需要 this as abyte[]
才能将其用作 aStream
然后将其插入到我们的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)
它会为您将其转换为图像。然后,您可以使用此代码Image
设置PictureBox
pictureBox1.Image = Decode("000001111100000111111100000011");
重要通知:ArgumentException was unhandled: Parameter is not valid.
由于二进制代码无效,您可能会收到这种情况。
谢谢,
我希望你觉得这有帮助:)