Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 4, 2024 03:21
Show Gist options
  • 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 PSDocument from PostScript file
PsDocument document = new PsDocument("input.ps");
// 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.Jpeg);
//Save PS to JPEG images bytes.
// 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 = document.SaveAsImage(options);
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 PSDocument from PostScript file
PsDocument document = new PsDocument("input.ps");
// 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.Png);
//Save PS to PNG images bytes.
// 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 = document.SaveAsImage(options);
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