Displaying Raw Data From Image File Using TextBox

2019-02-27 08:16发布

My program reads a DDS image file and stores it as a byte array. I want to be able to show the users the raw data in a TextBox form so at first I convert the byte array to a string using the following code:

string data = System.Text.Encoding.ASCII.GetString(bytes);

I then set the TextBox text:

textBox.Text = data;

The problem I am having is the text box is not showing all the data. Here is a screenshot of how it looks:

TextBox view

As you can see only the first few characters are displayed. I am assuming this is because the string contains a null terminator which the TextBox interprets as the end of the string. Here is a copy paste of the first 50 or so characters in the string which I copied directly from the debugger watch window:

DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\

As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.

What I want to be displayed is similar to what you see if you edit the raw DDS file with a text editor such as Notepadd++.

Opening the DDS file in Notepad++ produces the following:

Notepad++ view

My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?

1条回答
Animai°情兽
2楼-- · 2019-02-27 09:03

The simplest solution is to use this:

textbox.Text = data.Replace("\0", @"\0");

This will force the textbox to actually show a backslash followed by a zero where the nulls would be. Alternatively, you could replace the nulls with some other character or string.

查看更多
登录 后发表回答