You can read all the details at: Convert PostScript EPS/PS to PNG or JPG Image in C#
Last active
December 23, 2021 07:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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