convert a png file to a pcx file using c#
Asked Answered
E

1

6

I'm trying to convert a .png file to a .pcx file. The scenario is the following:

I'm using a TSC TTP-343C label printer. On the labels I have to print images. TSC provides a library documentation for developers. Since I can only print images on those labels using pcx files I have to convert all the images to pcx images. Any other format or even incorrect pcx format (e.g. if the user just renamed the file ending) will not be printed on the label.

I've seen 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. I tried to convert the images like:

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
}

Unfortunately this is not saving the image correctly. The label printer still cannot print the image on the label. I tried printing a correct pcx file and this worked fine. So I guess the only reason why it's still not working is that the converted file is not a real pcx file.

Is there a way to do such a conversion? If yes, how can I achieve that? My application is a windows forms application, written in C# using .NET framework 4.5.2.

EDIT:

Here you can see an example how to print a label with a pcx file:

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();

This code works fine on real pcx files. The methods of the TSC library can you find 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

EDIT II:

A pcx file which is working (created using photoshop) looks like this (if it helps you):

enter image description here

Eblis answered 3/3, 2016 at 16:13 Comment(3)
Did you see anything about which pcx format is ok? Or can you link toa working example? Will you need colors?Sparid
@Sparid I cannot find a specific format in the documentation. I added a working example (only working with correct converted pcx files) to my question, please have a look. Colors are not needed. The printer is not even able to print colors I guess.Eblis
@Sparid I added a screenshot of a PCX file which is working to my question. Hope it helps you.Eblis
S
6

PCX files are (at best) palette-based.

So to create a valid pcx output you need to add this one line:

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

Your image as pcx file

Sparid answered 4/3, 2016 at 8:54 Comment(1)
You don't know how thankful I am right now! Thank you very much.Eblis

© 2022 - 2024 — McMap. All rights reserved.