सी # के माध्यम से छवि फ़ाइलें रूपांतरण
क्रॉस-प्लेटफ़ॉर्म .NET आधारित एडवांस इमेज प्रोसेसिंग एप्लिकेशन बनाने के लिए इमेज फॉर्मेट, मेटाफाइल्स, वेबपी, एसवीजी, एपीएनजी को कन्वर्ट करें।
.NET इमेज एपीआई प्रोग्रामर्स के लिए उन्नत इमेज प्रोसेसिंग और रेंडरिंग सुविधाओं की सुविधा प्रदान करता है। डेवलपर्स इसे PSD, PDF, GIF, PNG, DICOM, SVG, JPG, JPEG2000, APNG, BMP, TIFF, HTML5 CANVAS, WEBP, WMF, EMF और अन्य छवि प्रारूपों में फ़ोटो और चित्रों सहित रेखापुंज और वेक्टर छवियों को परिवर्तित करने के लिए एकीकृत कर सकते हैं। . एपीआई न केवल फाइलों के रूपांतरण से संबंधित है, बल्कि छवियों को काले और सफेद और ग्रेस्केल में परिवर्तित करने, जीआईएफ छवि परतों को बदलने और भी बहुत कुछ करता है।
छवि को बिटमैप बीएमपी, जेपीजी, पीएनजी में कनवर्ट करें
सी # इमेज एपीआई का उपयोग करना, इंटर प्रारूप रूपांतरण उतना ही आसान है जितना वांछित प्रारूप के विस्तार को बदलना। यहां कुछ सामान्य मामले दिए गए हैं जैसे कि इमेज टू बीएमपी, इमेज टू जेपीजी, इमेज टू पीएनजी और डेवलपर्स अपने विशिष्ट फॉर्मेट के लिए आसानी से एन्हांस कर सकते हैं। प्रक्रिया स्रोत छवि को Image.Load के माध्यम से लोड करती है। किसी विशिष्ट सेटिंग के लिए लक्ष्य छवि प्रारूप विकल्प का ऑब्जेक्ट बनाएं। अंत में सेव मेथड को पाथ के साथ टारगेट फाइल पास करके और पैरामीटर के रूप में सेविंग ऑप्शन को कॉल करें।
छवियों के अंतर रूपांतरण के लिए सी # कोड
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); | |
} |
पीडीएफ रूपांतरण के लिए रेखापुंज छवि
रेखापुंज छवियों को पीडीएफ में बदलने की प्रक्रिया छवियों के अंतर रूपांतरण के समान है, सिवाय इसके कि एपीआई विशिष्ट पीडीएफ सेटिंग्स के लिए PdfOptions प्रदान करता है। . प्रोग्रामर अपनी विशिष्ट आवश्यकताओं के लिए इसे आसानी से बढ़ा सकते हैं।
पीडीएफ रूपांतरण के लिए रेखापुंज छवियों के लिए कोड
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
एसवीजी की रूपांतरण प्रक्रिया समान है, एसवीजी फ़ाइल लोड करें, प्रासंगिक छवि बचत विकल्पों का उपयोग करें और सेव विधि को कॉल करें। Image API SvgRasterizationOptions को PageWidth, PageHeight और रेखापुंज छवियों को सेट करने के लिए आरंभीकरण और SvgRasterizationOptions विकल्प प्राप्त करने के लिए अपनी VectorRasterizationOptions प्रॉपर्टी का उपयोग करता है।
सी # एसवीजी से रेखापुंज छवियों के लिए कोड
// 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); | |
} |
से परिवर्तित करने के लिए सभी समर्थित छवि प्रारूप
नीचे छवि प्रारूपों की पूरी सूची प्रस्तुत की गई है, जिन्हें आप इसमें बदल सकते हैं:
कनवर्ट करने के लिए सभी समर्थित छवि प्रारूप
नीचे छवि प्रारूपों की पूरी सूची प्रस्तुत की गई है, जिससे आप परिवर्तित कर सकते हैं: