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

Rom*_*man 6 c# png file-conversion winforms pcx

我正在尝试将.png文件转换为.pcx文件.方案如下:

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

我看过这篇文章链接到Magick图书馆.在这篇文章中,OP试图将bmp文件转换为pcx文件,这不是我试图实现的.我查看了有关转换图像Magick 文档.我试图将图像转换为:

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
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不能正确保存图像.标签打印机仍然无法在标签上打印图像.我尝试打印正确的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();
Run Code Online (Sandbox Code Playgroud)

此代码适用于真正的pcx文件.您可以在这里找到TSC库的方法.

downloadpcx(A,B)

描述:将单声道PCX图形文件下载到打印机参数:

a:字符串; 文件名(包括文件检索路径)

b:字符串,要在打印机内存中下载的文件名(请使用大写字母)

资料来源:http://www.tscprinters.com/cms/upload/download_en/DLL_instruction.pdf

编辑二:

正在运行的pcx文件(使用photoshop创建)看起来像这样(如果它可以帮助你):

在此输入图像描述

TaW*_*TaW 6

PCX文件(最好)基于调色板.

因此,要创建有效的pcx输出,您需要添加以下一行:

using (MagickImage image = new MagickImage(sourcePng))
{
    image.Format = MagickFormat.Pcx;
    image.ColorType = ColorType.Palette;  // <----
    image.Write(targetPcx);
}
Run Code Online (Sandbox Code Playgroud)

您的图像为pcx文件

  • 你不知道我现在多么感恩!非常感谢你. (3认同)