通過 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);
}

所有支持的圖像格式轉換

下面是完整的圖像格式列表,您可以將其轉換為:


要轉換為的所有支持的圖像格式

下面是完整的圖像格式列表,您可以從以下格式進行轉換: