Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/83de7b92a77c31e6ae7d610f4898e010 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/83de7b92a77c31e6ae7d610f4898e010 to your computer and use it in GitHub Desktop.
C# Convert PostScript EPS/PS to PNG or JPG Image Programmatically
// Initialize PostScript input stream
FileStream psStream = new FileStream("input.ps", FileMode.Open, FileAccess.Read);
PsDocument document = new PsDocument(psStream);
// If you want to convert Postscript file despite of minor errors set this flag
bool suppressErrors = true;
// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
ImageFormat imageFormat = ImageFormat.Jpeg;
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
ImageDevice device = new ImageDevice();
try
{
document.Save(device, options);
}
finally
{
psStream.Close();
}
// For every page an image bytes array will be obtained where number of byte arrays equals to the number of pages
// in input PS file.
byte[][] imagesBytes = device.ImagesBytes;
int i = 0;
foreach (byte[] imageBytes in imagesBytes)
{
string imagePath = Path.GetFullPath("image" + i.ToString() + "." + imageFormat.ToString());
using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
{
fs.Write(imageBytes, 0, imageBytes.Length);
}
i++;
}
// Initialize PostScript input stream
FileStream psStream = new FileStream("input.ps", FileMode.Open, FileAccess.Read);
PsDocument document = new PsDocument(psStream);
// If you want to convert Postscript file despite of minor errors set this flag
bool suppressErrors = true;
// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
ImageFormat imageFormat = ImageFormat.Png;
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
ImageDevice device = new ImageDevice();
try
{
document.Save(device, options);
}
finally
{
psStream.Close();
}
// For every page an image bytes array will be obtained where number of byte arrays equals to the number of pages
// in input PS file.
byte[][] imagesBytes = device.ImagesBytes;
int i = 0;
foreach (byte[] imageBytes in imagesBytes)
{
string imagePath = Path.GetFullPath("image" + i.ToString() + "." + imageFormat.ToString());
using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
{
fs.Write(imageBytes, 0, imageBytes.Length);
}
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment