我正在尝试将 .png
文件转换为 .pcx
文件。场景如下:
我使用的是 TSC TTP-343C 标签打印机。在标签上我必须打印图像。 TSC 提供了一个 library documentation对于开发人员。由于我只能使用 pcx 文件在这些标签上打印图像,因此我必须将所有图像转换为 pcx 图像。任何其他格式甚至不正确的 pcx 格式(例如,如果用户刚刚重命名文件结尾)都不会打印在标签上。
我看过 this post链接到 Magick
库。在这篇文章中,OP 试图将 bmp 文件转换为 pcx 文件,这并不是我想要实现的。我查看了 Magick
documentation关于converting images .我尝试将图像转换为:
using (MagickImage img = new MagickImage(png)) // png is a string containing the path of the .png file
{
img.Format = MagickFormat.Pcx;
img.Write(pcx); // pcx is a string containing the path of the new .pcx file
}
不幸的是,这没有正确保存图像。标签打印机仍然无法在标签上打印图像。我试着打印一个正确的 pcx 文件,这工作正常。所以我猜它仍然无法正常工作的唯一原因是转换后的文件不是真正的 pcx 文件。
有没有办法进行这样的转换?如果是,我该如何实现?我的应用程序是一个 Windows 窗体应用程序,使用 .NET Framework 4.5.2 用 C# 编写。
编辑:
在这里您可以看到一个如何使用 pcx 文件打印标签的示例:
TSC.openport(sPrinterName);
TSC.setup("100", "100", "4", "8", "1", "3.42", "0");
TSC.clearbuffer();
TSC.downloadpcx(@"\\PathToPcxFile\test.pcx", "test.pcx");
TSC.sendcommand("PUTPCX 35," + y + ",\"test.pcx\"");
TSC.printlabel("1", "1");
TSC.closeport();
此代码适用于真实的 pcx 文件。 TSC 库的方法你能找到吗 here .
downloadpcx(a,b)
Description: Download mono PCX graphic files to the printer Parameter:
a: string; file name (including file retrieval path)
b: string, names of files that are to be downloaded in the printer memory (Please use capital letters)
Source: http://www.tscprinters.com/cms/upload/download_en/DLL_instruction.pdf
编辑二:
一个正在工作的 pcx 文件(使用 photoshop 创建)看起来像这样(如果它对您有帮助的话):
最佳答案
PCX files是(充其量)基于调色板的。
因此,要创建有效的 pcx 输出,您需要添加这一行:
using (MagickImage image = new MagickImage(sourcePng))
{
image.Format = MagickFormat.Pcx;
image.ColorType = ColorType.Palette; // <----
image.Write(targetPcx);
}
关于c# - 使用 C# 将 png 文件转换为 pcx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35777327/