使用 C# 将 png 文件转换为 pcx 文件

convert a png file to a pcx file using c#

我正在尝试将 .png 文件转换为 .pcx 文件。场景如下:

我使用的是 TSC TTP-343C 标签打印机。在标签上我必须打印图像。 TSC 为开发者提供了 library documentation。由于我只能使用 pcx 文件在这些标签上打印图像,因此我必须将所有图像转换为 pcx 图像。任何其他格式甚至不正确的 pcx 格式(例如,如果用户刚刚重命名文件结尾)都不会打印在标签上。

我看过this post linking to the Magick library. In this post, the OP is trying to convert a bmp file to a pcx file which is not exactly what I try to achieve. I looked at the Magick documentation about 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);
}

Your image as pcx file