打开.prn文件,其中包含使用c#正确编码的图像

时间:2019-02-06 09:25:31

标签: c# image encoding character-encoding honeywell

我需要打开.prn文件并替换一些字符串。

在.prn文件中,我包含了一个图像,该图像具有这样的字符串: enter image description here

打开.prn文件时,C#无法按原样读取字符串。

可能丢失了一些编码,但不确定是哪种编码。

我尝试了不同的编码,但没有成功。

以下是在读取模式下打开文件的代码:

string text = File.ReadAllText(root + @"testImage.prn");

c#以这种方式读取该字符串

enter image description here

并且我无法打印包含图像的文件。

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

大多数PRN文件包含ISO编码。因此,尝试使用ISO编码,并使用System.IO.StreamReader显式指定所需的编码来读取文件。

以下示例在我的情况下非常有效:

System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
string text;
using (System.IO.StreamReader sr = new System.IO.StreamReader(path, encoding))
{
    text = sr.ReadToEnd();
}

答案 1 :(得分:0)

在 Java 中,它对我来说是这样的:使用流和字符集 ISO-8859-1。

Stream<String> stream = Files.lines(Paths.get(filePath), Charset.forName("ISO-8859-1"));