通过 C# 转换图像文件

转换图像格式、元文件、WebP、Svg、Apng 以构建基于 .NET 的跨平台高级图像处理应用程序。

 

.NET Image API 为程序​​员提供了高级图像处理和渲染功能。开发人员可以集成它来将光栅和矢量图像,包括照片和图片转换为 PSD、PDF、GIF、PNG、DICOM、SVG、JPG、JPEG2000、APNG、BMP、TIFF、HTML5 CANVAS、WEBP、WMF、EMF 和其他图像格式. API 不仅处理文件转换,还处理将图像转换为黑白和灰度,转换 GIF 图像层等等。

将图像转换为位图 BMP、JPG、PNG

使用 C# Image API,Inter 格式转换就像更改所需格式的扩展名一样简单。以下是一些通用案例,例如image to bmpimage to jpgimage to png,开发人员可以轻松地针对他们的特定格式进行增强。过程是通过 Image.Load 加载源图像。为任何特定设置创建目标 图像格式选项 的对象。最后调用 保存方法 ,通过传递带有路径和保存选项的目标文件作为参数。

图像相互转换的 C# 代码

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
ProcessConvertion();
void ProcessConvertion()
{
//Get list of supported formats in
//Aspose.Imaging for loading and saving images
var formats = GetAvailableImageFormats();
var importFormats = formats.Import;
var exportFormats = formats.Export;
//Process each raster and vector format that can be loaded
foreach (var format in importFormats)
{
string formatExt = format.Key;
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
//Process each raster and vector format
//to which we can save imported image
foreach (var exportFormat in exportFormats)
{
var outputFile = Path.Combine(templatesFolder, $"convert-{formatExt}-to-{exportFormat.Key}.{exportFormat.Key}");
System.Console.WriteLine("Processing conversion:" + outputFile);
//More about load method can be found at
//https://apireference.aspose.com/imaging/net/aspose.imaging.image/load/methods/2
//Load imported image
using (var image = Image.Load(inputFile))
{
//Obtain default saving options defined for each image
ImageOptionsBase exportOptions = exportFormat.Value.Clone();
//If loaded image is vector, need to specify vector rasterization options
//for export to another vector
if (image is VectorImage)
{
VectorRasterizationOptions rasterizationOptions = format.Value;
rasterizationOptions.PageWidth = image.Width;
rasterizationOptions.PageHeight = image.Height;
exportOptions.VectorRasterizationOptions = rasterizationOptions;
}
image.Save(outputFile, exportOptions);
}
File.Delete(outputFile);
}
//System.GC.Collect();
}
}
(Dictionary<string, VectorRasterizationOptions> Import, Dictionary<string, ImageOptionsBase> Export) GetAvailableImageFormats()
{
////////////////////////////////
///Raster and vector formats to that we can export images
////////////////////////////////
//Raster image formats that support both - save and load and their default save options
Dictionary<string, ImageOptionsBase> rasterFormatsThatSupportExportAndImport = new Dictionary<string, ImageOptionsBase>()
{
{ "bmp", new BmpOptions()},
{ "gif", new GifOptions()},
{ "dicom", new DicomOptions()},
{ "jpg", new JpegOptions()},
{ "jpeg", new JpegOptions()},
{ "jpeg2000", new Jpeg2000Options() },
{ "j2k", new Jpeg2000Options { Codec = Aspose.Imaging.FileFormats.Jpeg2000.Jpeg2000Codec.J2K } },
{ "jp2", new Jpeg2000Options { Codec = Aspose.Imaging.FileFormats.Jpeg2000.Jpeg2000Codec.Jp2 }},
{ "png",new PngOptions(){ ColorType = PngColorType.TruecolorWithAlpha} },
{ "apng", new ApngOptions()},
{ "tiff", new Aspose.Imaging.ImageOptions.TiffOptions(Aspose.Imaging.FileFormats.Tiff.Enums.TiffExpectedFormat.Default)},
{ "tif", new Aspose.Imaging.ImageOptions.TiffOptions(Aspose.Imaging.FileFormats.Tiff.Enums.TiffExpectedFormat.Default)},
{ "tga", new TgaOptions()},
{ "webp", new WebPOptions()},
{ "ico", new IcoOptions()}
};
//Vector image formats that support both - save and load, their default save options
//and their rasterization options when exporting to another vector image
Dictionary<string, (ImageOptionsBase, VectorRasterizationOptions)> vectorFormatsThatSupportExportAndImport
= new Dictionary<string, (ImageOptionsBase, VectorRasterizationOptions)>()
{
{ "emf", (new EmfOptions(),new EmfRasterizationOptions()) },
{ "svg", (new SvgOptions(), new SvgRasterizationOptions())},
{ "wmf", (new WmfOptions(), new WmfRasterizationOptions())},
{ "emz", (new Aspose.Imaging.ImageOptions.EmfOptions(){ Compress = true }, new EmfRasterizationOptions())},
{ "wmz", (new Aspose.Imaging.ImageOptions.WmfOptions(){ Compress = true }, new WmfRasterizationOptions())},
{ "svgz", (new Aspose.Imaging.ImageOptions.SvgOptions(){ Compress = true }, new SvgRasterizationOptions())},
};
////////////////////////////////
///Raster and vector formats from which we can load images
////////////////////////////////
//Formats that can be only saved (supported only save to this formats)
Dictionary<string, ImageOptionsBase> formatsOnlyForExport = new Dictionary<string, ImageOptionsBase>()
{
{ "psd", new PsdOptions()},
{ "dxf", new DxfOptions(){ TextAsLines = true,ConvertTextBeziers = true} },
{ "pdf", new PdfOptions()},
{ "html", new Html5CanvasOptions()},
};
//Raster formats that can be only loaded
List<string> formatsOnlyForImport = new List<string>()
{
"djvu", "dng", "dib"
};
//Vector formats only for loading and their rasterization options when exporting to another vector format
Dictionary<string, VectorRasterizationOptions> vectorFormatsOnlyForImport = new Dictionary<string, VectorRasterizationOptions>()
{
{"eps", new EpsRasterizationOptions()},
{"cdr", new CdrRasterizationOptions() },
{"cmx", new CmxRasterizationOptions() },
{"otg", new OtgRasterizationOptions() },
{"odg", new OdgRasterizationOptions() }
};
//Get total set of formats to what we can export images
Dictionary<string, ImageOptionsBase> exportFormats = vectorFormatsThatSupportExportAndImport
.ToDictionary(s => s.Key, s => s.Value.Item1)
.Union(formatsOnlyForExport)
.Concat(rasterFormatsThatSupportExportAndImport)
.ToDictionary(s => s.Key, s => s.Value);
//Get total set of formats that can be loaded
Dictionary<string, VectorRasterizationOptions> importFormats = vectorFormatsOnlyForImport
.Union(formatsOnlyForImport.ToDictionary(s => s, s => new VectorRasterizationOptions()))
.Union(vectorFormatsThatSupportExportAndImport.ToDictionary(s => s.Key, s => s.Value.Item2))
.ToDictionary(s => s.Key, s => s.Value);
return (Import: importFormats, Export: exportFormats);
}

光栅图像到 PDF 转换

将光栅图像转换为 PDF 的过程与图像相互转换的过程相同,只是 API 为特定的 PDF 设置提供了 PdfOptions .程序员可以根据他们的特定需求轻松地对其进行增强。

光栅图像到 PDF 转换的代码

using (Image imge = Image.Load(dataDir+ "transparent_orig.gif"))
{
imge.Save(dataDir+"output.pdf", new PdfOptions() { PdfDocumentInfo = new Aspose.Imaging.FileFormats.Pdf.PdfDocumentInfo() } );
}

将 SVG 转换为光栅图像 BMP、PNG、JPG

SVG的转换过程是一样的,加载SVG文件,使用相关的图片保存选项,调用Save方法。 Image API 提供 SvgRasterizationOptions 用于设置 PageWidth、PageHeight 和光栅图像,使用它们的 VectorRasterizationOptions 属性进行初始化和获取 SvgRasterizationOptions 选项。

用于 SVG 到光栅图像的 C# 代码

// Load the image
using (SvgImage image = (SvgImage)Image.Load(dataDir + "sourceFile.Svg"))
{
// Create an instance of relevant raster image options and Save the results to disk
PngOptions pngOptions = new PngOptions();
SvgRasterizationOptions svgOptions = new SvgRasterizationOptions();
svgOptions.PageWidth = 100;
svgOptions.PageHeight = 200;
pngOptions.VectorRasterizationOptions = svgOptions;
image.Save(dataDir + "ConvertingSVGToRasterImages_out.png", pngOptions);
}

所有支持的图像格式转换

下面是完整的图像格式列表,您可以将其转换为:


要转换为的所有支持的图像格式

下面是完整的图像格式列表,您可以从中进行转换: