Setting Properties on Images
Applying Smoothing Mode
Smoothing is often used to reduce noise within an image. VectorRasterizationOptions.SmoothingMode is a property used for improving the quality of images. This article demonstrates how to apply VectorRasterizationOptions.SmoothingMode property to images using Aspose.Imaging for .NET.
Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Cdr; | |
using Aspose.Imaging.FileFormats.Cmx; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.OpenDocument; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string[] files = new string[] { | |
"template.cdr", | |
"template.cmx", | |
"template.emf", | |
"template.wmf", | |
"template.odg", | |
"template.svg" | |
}; | |
SmoothingMode[] smoothingModes = new SmoothingMode[] { | |
SmoothingMode.AntiAlias, SmoothingMode.None | |
}; | |
foreach (string fileName in files) | |
{ | |
using (Image image = Image.Load(dataDir + fileName)) | |
{ | |
VectorRasterizationOptions vectorRasterizationOptions; | |
if (image is CdrImage) | |
{ | |
vectorRasterizationOptions = new CdrRasterizationOptions(); | |
} | |
else if (image is CmxImage) | |
{ | |
vectorRasterizationOptions = new CmxRasterizationOptions(); | |
} | |
else if (image is EmfImage) | |
{ | |
vectorRasterizationOptions = new EmfRasterizationOptions(); | |
} | |
else if (image is WmfImage) | |
{ | |
vectorRasterizationOptions = new WmfRasterizationOptions(); | |
} | |
else if (image is OdgImage) | |
{ | |
vectorRasterizationOptions = new OdgRasterizationOptions(); | |
} | |
else if (image is SvgImage) | |
{ | |
vectorRasterizationOptions = new SvgRasterizationOptions(); | |
} | |
else | |
{ | |
throw new Exception("This is image is not supported in this example"); | |
} | |
vectorRasterizationOptions.PageSize = image.Size; | |
foreach (SmoothingMode smoothingMode in smoothingModes) | |
{ | |
string outputFileName = dataDir + smoothingMode + "_" + fileName + ".png"; | |
vectorRasterizationOptions.SmoothingMode = smoothingMode; | |
image.Save(outputFileName, new PngOptions() | |
{ | |
VectorRasterizationOptions = vectorRasterizationOptions | |
}); | |
File.Delete(outputFileName); | |
} | |
} | |
} |
Improve Text Quality
VectorRasterizationOptions.TextRenderingHint is a property used for improving the quality of the text. This article demonstrates how to apply VectorRasterizationOptions.TextRenderingHint property to images using Aspose.Imaging for .NET.
Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Cdr; | |
using Aspose.Imaging.FileFormats.Cmx; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.OpenDocument; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string[] files = new string[] { | |
"template.cdr", | |
"template.cmx", | |
"template.emf", | |
"template.wmf", | |
"template.odg", | |
"template.svg" | |
}; | |
TextRenderingHint[] textRenderingHints = new TextRenderingHint[] { | |
TextRenderingHint.AntiAlias, TextRenderingHint.AntiAliasGridFit, | |
TextRenderingHint.ClearTypeGridFit, TextRenderingHint.SingleBitPerPixel, TextRenderingHint.SingleBitPerPixelGridFit | |
}; | |
foreach (string fileName in files) | |
{ | |
using (Image image = Image.Load(dataDir + fileName)) | |
{ | |
VectorRasterizationOptions vectorRasterizationOptions; | |
if (image is CdrImage) | |
{ | |
vectorRasterizationOptions = new CdrRasterizationOptions(); | |
} | |
else if (image is CmxImage) | |
{ | |
vectorRasterizationOptions = new CmxRasterizationOptions(); | |
} | |
else if (image is EmfImage) | |
{ | |
vectorRasterizationOptions = new EmfRasterizationOptions(); | |
} | |
else if (image is WmfImage) | |
{ | |
vectorRasterizationOptions = new WmfRasterizationOptions(); | |
} | |
else if (image is OdgImage) | |
{ | |
vectorRasterizationOptions = new OdgRasterizationOptions(); | |
} | |
else if (image is SvgImage) | |
{ | |
vectorRasterizationOptions = new SvgRasterizationOptions(); | |
} | |
else | |
{ | |
throw new Exception("This is image is not supported in this example"); | |
} | |
vectorRasterizationOptions.PageSize = image.Size; | |
foreach (TextRenderingHint textRenderingHint in textRenderingHints) | |
{ | |
string outputFileName = dataDir + textRenderingHint + "_" + fileName + ".png"; | |
vectorRasterizationOptions.TextRenderingHint = textRenderingHint; | |
image.Save(outputFileName, new PngOptions() | |
{ | |
VectorRasterizationOptions = vectorRasterizationOptions | |
}); | |
File.Delete(outputFileName); | |
} | |
} | |
} |
Support of image scoped fonts
Aspose.Imaging .NET API allows the custom font source providing to use the specific font(s) for image rendering. Unlike FontSettings.SetFontsFolders method works in the image scope and allowing to provide the fonts in multi-user scenarios.
Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Cdr; | |
using Aspose.Imaging.FileFormats.Cmx; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.OpenDocument; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string[] files = new string[] { "template.emf", "template.odg", "template.wmf", "template.svg" }; | |
foreach (var file in files) | |
{ | |
string outputPath = Path.Combine(dataDir, file + ".png"); | |
CustomFontSourceTest(dataDir, dataDir, file, Path.Combine(dataDir, "Fonts")); | |
File.Delete(outputPath); | |
} | |
static void CustomFontSourceTest(string inputPath, string outputPath, string fileName, string fontPath) | |
{ | |
var loadOptions = new Aspose.Imaging.LoadOptions(); | |
loadOptions.AddCustomFontSource(GetFontSource, fontPath); | |
using (var img = Image.Load(Path.Combine(inputPath, fileName), loadOptions)) | |
{ | |
Aspose.Imaging.ImageOptions.VectorRasterizationOptions vectorRasterizationOptions = | |
(Aspose.Imaging.ImageOptions.VectorRasterizationOptions)img.GetDefaultOptions(new object[] { Color.White, img.Width, img.Height }); | |
vectorRasterizationOptions.TextRenderingHint = Aspose.Imaging.TextRenderingHint.SingleBitPerPixel; | |
vectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None; | |
img.Save(Path.Combine(outputPath, fileName + ".png"), new Aspose.Imaging.ImageOptions.PngOptions | |
{ | |
VectorRasterizationOptions = vectorRasterizationOptions | |
}); | |
File.Delete(Path.Combine(outputPath, fileName + ".png")); | |
} | |
} | |
// The custom fonts provider example. | |
static Aspose.Imaging.CustomFontHandler.CustomFontData[] GetFontSource(params object[] args) | |
{ | |
string fontsPath = string.Empty; | |
if (args.Length > 0) | |
{ | |
fontsPath = args[0].ToString(); | |
} | |
var customFontData = new List<Aspose.Imaging.CustomFontHandler.CustomFontData>(); | |
foreach (var font in Directory.GetFiles(fontsPath)) | |
{ | |
customFontData.Add(new Aspose.Imaging.CustomFontHandler.CustomFontData(Path.GetFileNameWithoutExtension(font), File.ReadAllBytes(font))); | |
} | |
return customFontData.ToArray(); | |
} |