Helpful resources ▼
使用 .NET API 转换 EPS 文件
Contents
[
Hide
Show
]本文以 EPS 到 BMP 格式转换为例,讲解如何使用 C# 转换 EPS 文件。
Aspose.Page 允许您使用 C# 将 EPS 文件转换为 BMP、JPG、EMF、TIFF、PNG、WMF 和 PDF 格式。
C# EPS 到 BMP 转换
您可以使用免费在线 EPS 到 BMP 转换器、EPS 查看器 和其他 用于处理 EPS 文件的应用程序
Aspose.Page for .NET EPS 转换器允许使用 .NET 平台支持的任何语言(C#、VB、J#)转换封装的 PostScript (EPS) 文件。所有转换均以与以下示例类似的方式执行。
执行 EPS 到 BMP 转换的步骤:
- 从 EPS 文件创建 PsDocument 实例。
- 使用 ImageSaveOptions(如果转换为 PDF,则使用 PdfSaveOptions)指定 AdditionalFontsFolder 和 SuppressError 布尔值。
- 将 PostScript 文档保存为图像,并使用图像保存选项将其保存为字节数组。输入文档的一页对应一个字节数组。
- 将生成的二维字节数组保存为 BMP 文件,并为每个字节数组创建一个新的文件输出流。
- 如果 SuppressErrors 值为 true(默认情况下),则可以查看 EPS 到 BMP 转换过程中抛出的错误。
以下 C# 代码片段展示了如何在 C# 中将 EPS 文件转换为 BMP 文件。所有其他 EPS 转换均以类似的方式完成。
{{#if_output 'BMP'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Bmp;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as BMP image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'EMF'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Emf;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as EMF image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'JPG'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Jpeg;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as JPEG image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'PDF'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "input.eps");
// If you want to convert Postscript file despite of minor errors set this flag
bool suppressErrors = true;
//Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Default page size is 595x842 and it is not mandatory to set it in PdfSaveOptions
// But if you need to specify sizeuse following line
//PdfSaveOptions options = new PdfSaveOptions(suppressErrorsnew, Aspose.Page.Drawing.Size(595x842));
// or
//saveOptions.Size = new Aspose.Page.Drawing.Size(595x842);
//Save EPS as PDF document
document.SaveAsPdf(dataDir + "outputPDF_out.pdf", options);
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'PNG'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Png;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as PNG image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'TIFF'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Tiff;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as TIFF image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
{{#if_output 'WMF'}}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with the name of EPS file.
PsDocument document = new PsDocument(dataDir + "inputForImage.eps");
// 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();
//Set output image format.
options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Wmf;
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save EPS as WMF image.
document.SaveAsImage(options, outputDir, "out_image");
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
{{/if_output}}
我们来看一下 ImageSaveOptions。使用这个类,我们可以在将 EPS 转换为 BMP 时指定不同的转换参数。
- Size 指定生成的图像的宽度和高度。
- ImageFormat 指定输出图像的格式。
- SmoothingMode 控制在生成的图像中绘制曲线、文本和光栅图像的质量。
- AdditionalFontsFolder 指定字体的保存位置。默认情况下,系统字体文件夹始终包含在内。
- SuppressError 控制 EPS 到 BMP 转换器在出现非关键错误时的行为。如果该值为 true,则可以在转换后在 Exceptions 字段中查看此类错误列表。默认值为 true。
- Debug 允许将调试信息输出到控制台。默认值为 false。