Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a png file to a pcx file using c#

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

like image 838
roemel Avatar asked Mar 03 '16 16:03

roemel


People also ask

How do I convert PNG files?

Go to File > Save as and open the Save as type drop-down menu. You can then select JPEG and PNG, as well as TIFF, GIF, HEIC, and multiple bitmap formats. Save the file to your computer and it will convert.

What is PCX file format used for?

PCX, an acronym for Picture Exchange, is a raster image file type, which means that it uses color pixels to display images. The other main type of image format is vector, which creates visuals using a complex structure of points, lines, graphs, and formulas.


1 Answers

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

like image 192
TaW Avatar answered Oct 03 '22 14:10

TaW