使用TextBox或RichTextBox从图像文件显示原始数据? [英] Displaying Raw Data From Image File Using TextBox or RichTextBox?
问题描述
我的程序读取DDS图像文件并将其存储为字节数组.我希望能够以文本框形式向用户显示原始数据,因此首先我使用以下代码将字节数组转换为字符串:
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);
然后我设置TextBox文本:
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将该终止符解释为字符串的末尾.这是我从调试器监视窗口直接复制的字符串中前50个左右字符的复制粘贴:
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 \ 0 \
DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
如您所见,第一个空字符紧接在"DDS |"之后解释了为什么这就是在文本框中显示的全部内容.
As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.
我要显示的内容类似于使用文本编辑器(例如Notepadd ++)编辑原始DDS文件时看到的内容.
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++.
在Notepad ++中打开DDS文件将产生以下内容:
Opening the DDS file in Notepad++ produces the following:
我的问题是,如何使我的TextBox(或RichTextBox)以与Notepad ++显示数据相同的方式显示数据?
My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?
推荐答案
最简单的解决方案是使用此方法:
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.
这篇关于使用TextBox或RichTextBox从图像文件显示原始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!