Displaying Raw Data From Image File Using TextBox or RichTextBox?

强颜欢笑 提交于 2019-12-02 06:17:55

问题


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:

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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/17352442/displaying-raw-data-from-image-file-using-textbox-or-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!