2
votes

I have the ghostscript dll (gsdll32.dll) which I have wrapped into my c# application. I tried various way to convert postscript to jpeg but it's not happening. The code is as follows:

            PDFPrinter.WGhostScript gs = new PDFPrinter.WGhostScript();
            gs.AddParam("-sDEVICE=jpeg");
            gs.AddParam("-dJPEGQ=100");
            gs.AddParam("-dNOPAUSE");
            gs.AddParam("-dBATCH");
            gs.AddParam("-dSAFER");
            gs.AddParam("-r300");
            string outfile = txtOutFolderLoc.Text + txtFileName.Text + ".jpg";
            gs.AddParam(@"-sOutputFile=" + outfile);
            gs.AddParam(psFilePath);
            gs.Execute();
            Application.Exit();

What might be the reasons?

  1. I have the postscript location at hand in the string "psFilePath".
  2. "outfile" represents the location and file name of the output.

[I have used the same stuff and converted the postcript to PDF and PNG as following].

To PDF WORKED

            gs.AddParam("-dBATCH");
            gs.AddParam("-dNOPAUSE");
            gs.AddParam("-sDEVICE=pdfwrite");
            gs.AddParam("-sPAPERSIZE=a4");
            gs.AddParam("-sProcessColorModel=DeviceGray");
            gs.AddParam("-sPDFPassword=password");
            string outfile = txtOutFolderLoc.Text + txtFileName.Text + ".pdf";
            gs.AddParam(@"-sOutputFile=" + outfile);
            gs.AddParam(psFilePath);
            gs.Execute();
            Application.Exit();

TO PNG CODE:

            gs.AddParam("-dSAFER");
            gs.AddParam("-dBATCH");
            gs.AddParam("-dNOPAUSE");
            gs.AddParam("-sDEVICE=png16m");
            gs.AddParam("-dGraphicsAlphaBits=4");
            gs.AddParam(@"-sOutputFile=" + txtOutFolderLoc.Text + txtFileName.Text + "%i.png");
            gs.AddParam(psFilePath);
            gs.Execute();
            Application.Exit();

EDIT I The postscript is being generated and the application continues till it exits. But no jpeg file is found.

The postscript is generated by a postscript printer provided with the ghostscript. Once this postscript is generated the control is transferred to the application that converts this PS.

1
can you elaborate on "its not happening." no file, empty file, wrong contents? and all you've changed between the working PDF and PNG versions is the -sDEVICE? could the postscript be missing showpage? that can lead to inconsistent results unless you run it through ps2eps first. - luser droog
@luser droog: Thanks for you reply. I've edited the content & I've added the code to convert the PS to PDF and PNG. Both the functions are working fine. But the JPEG code seems like its not generating the output file. - user1976596
That's about all I know from having used the commandline version for these types of conversions. But it's a much stronger, more answerable question now. Good job. +1 Welcome to the site! - luser droog
Thank you very much @luser droog for your support - user1976596
are you processing printer-driver output? that might have hardware control code in it that is unsupported by pdf. Some things to try: run gs from command line, so you will see any error messages; 2 test your program code with very clean/simple postscript code. - agentp

1 Answers

1
votes

I managed to get the output with the following arguments:

            PDFPrinter.WGhostScript gs = new PDFPrinter.WGhostScript();
            gs.AddParam("-q");
            gs.AddParam("-dNOPAUSE");
            gs.AddParam("-dBATCH");
            gs.AddParam("-sDEVICE=jpeg");
            gs.AddParam(@"-sOutputFile=<full oytput file path>%i.jpg");
            gs.AddParam(<psFilePath>);

I had to add in the "-q" to get it done.

'-q to prevent Ghostscript from writing messages to standard output which become mixed with the intended output stream.'