這是可能的。實際上,您需要將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