问题描述
我的程序读取 DDS 图像文件并将其存储为字节数组.我希望能够以 TextBox 形式向用户显示原始数据,因此首先我使用以下代码将字节数组转换为字符串:
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 | a
DDS | a
如您所见,第一个空字符紧跟在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(" ", @" ");
这将强制文本框实际显示一个反斜杠,后跟一个零,即空值所在的位置.或者,您可以将空值替换为其他字符或字符串.
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 显示图像文件中的原始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!