Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 15, 2022 09:56
Show Gist options
  • Save aspose-com-gists/de57973337b89375382bf6c74106c220 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/de57973337b89375382bf6c74106c220 to your computer and use it in GitHub Desktop.
#### 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.
Interested ?
You may go further at :
https://products.aspose.com/imaging/net/ or at https://github.com/aspose-imaging/Aspose.Imaging-for-.NET
using Aspose.Imaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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);
}
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.
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment