This is possible. You’ll actually need to convert 000001111100000111111100000011
or 000111000111000111000111000111
which will be treated as string
to a byte array byte[]
. To do this, we’ll use 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 :)
}
This will declare a new string input
and then encode it to a byte array (byte[]
). We’ll actually need this as a byte[]
to be able to use it as a Stream
and then insert it into our PictureBox
. To do this, we’ll use MemoryStream
MemoryStream FromBytes = new MemoryStream(bytes); //Create a new stream with a buffer (bytes)
Finally, we can simply use this stream to create our Image
file and set the file to our PictureBox
pictureBox1.Image = Image.FromStream(FromBytes); //Set the image of pictureBox1 from our stream
Example
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);
}
By using this, you can whenever call, for example Decode(000001111100000111111100000011)
and it will convert it to an image for you. Then, you may use this code to set the Image
property of a PictureBox
pictureBox1.Image = Decode("000001111100000111111100000011");
IMPORTANT NOTICE: You may receive ArgumentException was unhandled: Parameter is not valid.
this might happen due to an invalid Binary code.
Thanks,
I hope you find this helpful 🙂
solved Extract numbers out of a textbox [closed]