我已经为此苦苦挣扎了一段时间。我创建了一个实用程序,允许您打开 .TXT 文件。这些文本文件包含 PCL(打印命令语言)。当我导入一个新文件时,它被 \0(NULL 终止符)截断。因为 PCL 文件在我导入的所有内容中随机包含图形图像,所以在第一个位图图像处被截断,因为位图图像以 NULL 开头。
这是此处看到的确切问题:Displaying Raw Data From Image File Using TextBox or RichTextBox?
不幸的是,由于我的低(新手)声誉(需要 15 个代表),我无法评论这个线程。也无法粘贴屏幕截图(需要 10 个代表)。
以下是 Notepad++ 显示信息的方式:
这是我的 RichTextBox 显示相同信息的方式:
这就是为什么这是一个问题(缩小):
栅格数据正好在我需要的两部分数据(PCL)之间。栅格数据下方的所有信息都不会被引入。
这是我尝试过的(注意:我使用的是自定义 RichTextBox,但这不会影响任何事情,因为它只是一个具有拖放功能的 RichTextBox):
byte[] bytes = new byte[2048];
string data = System.Text.Encoding.ASCII.GetString(bytes);
dragDropRichTextBox1.Text = data.Replace("\0", @"1");
这只会导致 2048 个数字“1”字符链,没有任何文本文件的数据拉入。非常感谢任何帮助。
无论我做什么,我都想保留我当前的拖放功能:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PCL_Utility
{
public class DragDropRichTextBox : RichTextBox
{
public DragDropRichTextBox()
{
this.AllowDrop = true;
this.DragDrop += DragDropRichTextBox_DragDrop;
}
void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
{
//string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];
string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];
if (fileText != null)
{
foreach (string name in fileText)
{
try
{
this.AppendText(File.ReadAllText(name) + "\n -------- End of File -------- \n\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}