Manipulating Metafiles
Converting EMF To PDF
Using Aspose.Imaging for .NET, developers can convert EMF metafile to PDF format. This topic explains the approach to load existing metafiles and convert it to PDF. Aspose.Imaging for .NET provides the EmfImage class to load EMF files and same can be used to save the image to PDF format. Below provided sample code demonstrate how to convert EMF to PDF.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string[] filePaths = { | |
"template.emf" | |
}; | |
foreach (string filePath in filePaths) | |
{ | |
string outPath = dataDir + "result.pdf"; | |
using (var image = (EmfImage)Image.Load(dataDir + filePath)) | |
using (FileStream outputStream = new FileStream(outPath, FileMode.Create)) | |
{ | |
if (!image.Header.EmfHeader.Valid) | |
{ | |
throw new ImageLoadException(string.Format("The file {0} is not valid", outPath)); | |
} | |
EmfRasterizationOptions emfRasterization = new EmfRasterizationOptions(); | |
emfRasterization.PageWidth = image.Width; | |
emfRasterization.PageHeight = image.Height; | |
emfRasterization.BackgroundColor = Color.WhiteSmoke; | |
PdfOptions pdfOptions = new PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = emfRasterization; | |
image.Save(outputStream, pdfOptions); | |
} | |
} | |
File.Delete(dataDir + "result.pdf"); |
Cropping EMF Image
Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Cropping may also be used to cut out some portion of an image to increase the focus on a particular area. Aspose.Imaging for .NET API supports two different approaches for cropping image: by shifts and by rectangle.
Using Shifts
The EmfImage class provides an overloaded version of the Crop method that accepts 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the Crop method moves the image boundaries toward the center of the image while discarding the outer portion.
The code snippet below demonstrates how to crop an EMF image by shifts.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Create an instance of Rasterization options | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.WhiteSmoke; | |
// Create an instance of PNG options | |
PdfOptions pdfOptions = new PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = emfRasterizationOptions; | |
// Load an existing image into an instance of EMF class | |
using (EmfImage image = (EmfImage)Image.Load(dataDir + "template.emf")) | |
{ | |
using (FileStream outputStream = new FileStream(dataDir + "result.pdf", FileMode.Create)) | |
{ | |
// Based on the shift values, apply the cropping on image and Crop method will shift the image bounds toward the center of image | |
image.Crop(0, 0, 30, 30); | |
// Set height and width and Save the results to disk | |
pdfOptions.VectorRasterizationOptions.PageWidth = image.Width; | |
pdfOptions.VectorRasterizationOptions.PageHeight = image.Height; | |
image.Save(outputStream, pdfOptions); | |
} | |
} | |
File.Delete(dataDir + "result.pdf"); |
Using Rectangle
The EmfImage class provides another overloaded version of the Crop method that accepts an instance of the Rectangle class. You can cut out any portion of an image by providing the desired boundaries to the Rectangle object. The code snippet below demonstrates how.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Create an instance of Rasterization options | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.WhiteSmoke; | |
// Create an instance of PNG options | |
PdfOptions pdfOptions = new PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = emfRasterizationOptions; | |
// Load an existing image into an instance of EMF class | |
using (EmfImage image = (EmfImage)Image.Load(dataDir + "template.emf")) | |
{ | |
using (FileStream outputStream = new FileStream(dataDir + "result.pdf", FileMode.Create)) | |
{ | |
// Based on the shift values, apply the cropping on image and Crop method will shift the image bounds toward the center of image | |
image.Crop(0, 0, 30, 30); | |
// Set height and width and Save the results to disk | |
pdfOptions.VectorRasterizationOptions.PageWidth = image.Width; | |
pdfOptions.VectorRasterizationOptions.PageHeight = image.Height; | |
image.Save(outputStream, pdfOptions); | |
} | |
} | |
File.Delete(dataDir + "result.pdf"); |
Export MetaFile To Raster Formats
Using Aspose.Imaging for .NET, developers can convert metafile (Emf/Emf+) to raster formats. This article shows how to export/convert metafile file to raster image formats with Aspose.Imaging. Aspose.Imaging for .NET provides the EmfImage class to load EMF files and same can be used to convert the Emf to raster formats. Below provided sample code demonstrate how to convert Emf/Emf+ to raster images.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Create EmfRasterizationOption class instance and set properties | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.PapayaWhip; | |
emfRasterizationOptions.PageWidth = 300; | |
emfRasterizationOptions.PageHeight = 300; | |
// Load an existing EMF file as iamge and convert it to EmfImage class object | |
using (var image = (EmfImage)Image.Load(dataDir + "template.emf")) | |
{ | |
if (!image.Header.EmfHeader.Valid) | |
{ | |
throw new ImageLoadException(string.Format("The file {0} is not valid", dataDir + "Picture1.emf")); | |
} | |
// Convert EMF to BMP, GIF, JPEG, J2K, PNG, PSD, TIFF and WebP | |
image.Save(dataDir + "result.bmp", new BmpOptions { VectorRasterizationOptions = emfRasterizationOptions }); | |
image.Save(dataDir + "result.gif", new GifOptions { VectorRasterizationOptions = emfRasterizationOptions }); | |
image.Save(dataDir + "result.jpg", new JpegOptions { VectorRasterizationOptions = emfRasterizationOptions }); | |
} | |
File.Delete(dataDir + "result.bmp"); | |
File.Delete(dataDir + "result.gif"); | |
File.Delete(dataDir + "result.jpg"); |
Specifying Font Folder While Converting
Using Aspose.Imaging for .NET, developers can specify the font folder path used while converting WMF file. This topic explains the approach to specify the font folder that should be used. The following code snippet demonstrates the usage of Aspose.Imaging for .NET API to change the font while converting EMF to raster image.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.WhiteSmoke; | |
// Create an instance of PNG options | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.VectorRasterizationOptions = emfRasterizationOptions; | |
// Load an existing EMF image | |
using (EmfImage image = (EmfImage)Image.Load(dataDir + "template.emf")) | |
{ | |
image.CacheData(); | |
// Set height and width, Reset font settings | |
pngOptions.VectorRasterizationOptions.PageWidth = 300; | |
pngOptions.VectorRasterizationOptions.PageHeight = 350; | |
FontSettings.Reset(); | |
image.Save(dataDir + "result.png", pngOptions); | |
// Initialize font list | |
List<string> fonts = new List<string>(FontSettings.GetDefaultFontsFolders()); | |
// Add new font path to font list and Assign list of font folders to font settings and Save the EMF file to PNG image with new font | |
fonts.Add(dataDir + @"fonts\"); | |
FontSettings.SetFontsFolders(fonts.ToArray(), true); | |
image.Save(dataDir + "result2.png", pngOptions); | |
} | |
File.Delete(dataDir + "result.png"); | |
File.Delete(dataDir + "result2.png"); |
Support For Replacing Missing Fonts
Using Aspose.Imaging for .NET, developers can replace missing fonts when saving ODG, SVG and MetaFile images. This topic explains the approach to replace the fonts that should be used. The following code snippet demonstrates the usage of Aspose.Imaging for .NET API to replace the font while saving ODG images.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.WhiteSmoke; | |
// Create an instance of PNG options | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.VectorRasterizationOptions = emfRasterizationOptions; | |
// Load an existing EMF image | |
using (EmfImage image = (EmfImage)Image.Load(dataDir + "template.emf")) | |
{ | |
image.CacheData(); | |
// Set height and width, Reset font settings | |
pngOptions.VectorRasterizationOptions.PageWidth = 300; | |
pngOptions.VectorRasterizationOptions.PageHeight = 350; | |
FontSettings.Reset(); | |
image.Save(dataDir + "result.png", pngOptions); | |
// Initialize font list | |
List<string> fonts = new List<string>(FontSettings.GetDefaultFontsFolders()); | |
// Add new font path to font list and Assign list of font folders to font settings and Save the EMF file to PNG image with new font | |
fonts.Add(dataDir + @"fonts\"); | |
FontSettings.SetFontsFolders(fonts.ToArray(), true); | |
image.Save(dataDir + "result2.png", pngOptions); | |
} | |
File.Delete(dataDir + "result.png"); | |
File.Delete(dataDir + "result2.png"); |
Managing resources while saving Metafile as SVG
Using Aspose.Imaging for .NET, developers can manage resources (fonts and internal images) when saving Metafile images (emf and wmf) into SVG file stream. This topic explains that there should be a setting that will let us specify whether to save contained image as a separate file and provide a stream for saving it, or to embed image as base64 string inside of SVG. The following code snippet demonstrates the usage of Aspose.Imaging for .NET API to manage resources.
using Aspose.Imaging; | |
using Aspose.Imaging.CoreExceptions; | |
using Aspose.Imaging.Exif; | |
using Aspose.Imaging.Exif.Enums; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Djvu; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Pdf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Tga; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging.Xmp; | |
using Aspose.Imaging.Xmp.Schemas.Dicom; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
#region Methods | |
void SaveWithEmbeddedImages() | |
{ | |
string[] files = new string[1] { dataDir + "template.svg" }; | |
for (int i = 0; i < files.Length; i++) | |
{ | |
Save(true, files[i], null); | |
} | |
} | |
void SaveWithExportImages() | |
{ | |
string[] files = new string[1] { dataDir + "template.svg" }; | |
string[][] expectedImages = new string[1][] | |
{ | |
new string[1] | |
{ | |
"result.png" | |
}, | |
}; | |
for (int i = 0; i < files.Length; i++) | |
{ | |
Save(false, files[i], expectedImages[i]); | |
} | |
} | |
void Save(bool useEmbedded, string fileName, string[] expectedImages) | |
{ | |
string storeType = useEmbedded ? "Embedded" : "Stream"; | |
string inputFile = fileName; | |
string outFileName = dataDir + "result.svg"; | |
string outputFile = outFileName; | |
string imageFolder = string.Empty; | |
using (Image image = Image.Load(inputFile)) | |
{ | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.White; | |
emfRasterizationOptions.PageWidth = image.Width; | |
emfRasterizationOptions.PageHeight = image.Height; | |
string testingFileName = Path.GetFileNameWithoutExtension(inputFile); | |
imageFolder = Path.Combine(dataDir, testingFileName); | |
image.Save(outputFile, | |
new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
Callback = | |
new SvgCallbackImageTest(useEmbedded, imageFolder) | |
{ | |
Link = "Images/" + testingFileName | |
} | |
}); | |
} | |
if (!useEmbedded) | |
{ | |
string[] files = Directory.GetFiles(dataDir + "Images"); | |
if (files.Length != expectedImages.Length) | |
{ | |
throw new Exception(string.Format("Expected count font files = {0}, Current count image files = {1}", expectedImages.Length, files.Length)); | |
} | |
for (int i = 0; i < files.Length; i++) | |
{ | |
string file = Path.GetFileName(files[i]); | |
if (string.IsNullOrEmpty(file)) | |
{ | |
throw new Exception(string.Format("Expected file name: '{0}', current is empty", expectedImages[i])); | |
} | |
if (file.ToLower() != expectedImages[i]) | |
{ | |
throw new Exception(string.Format("Expected file name: '{0}', current: '{1}'", expectedImages[i], file.ToLower())); | |
} | |
} | |
} | |
} | |
#endregion | |
SaveWithEmbeddedImages(); | |
//SaveWithExportImages(); | |
File.Delete(dataDir + "result.png"); | |
File.Delete(dataDir + "result2.png"); | |
class SvgCallbackImageTest : Aspose.Imaging.FileFormats.Svg.SvgResourceKeeperCallback | |
{ | |
#region Fields | |
/// <summary> | |
/// The out folder | |
/// </summary> | |
private readonly string outFolder; | |
/// <summary> | |
/// The use embedded font | |
/// </summary> | |
private readonly bool useEmbeddedImage; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SvgTests.SvgCallbackFontTest" /> class. | |
/// </summary> | |
/// <param name="useEbeddedImage">if set to <c>true</c> [use ebedded image].</param> | |
/// <param name="outFolder">The out folder.</param> | |
public SvgCallbackImageTest(bool useEbeddedImage, string outFolder) | |
{ | |
this.useEmbeddedImage = useEbeddedImage; | |
this.outFolder = outFolder; | |
} | |
#endregion | |
#region Properties | |
public string Link { get; set; } | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Called when image resource ready. | |
/// </summary> | |
/// <param name="imageData">The resource data.</param> | |
/// <param name="imageType">Type of the image.</param> | |
/// <param name="suggestedFileName">Name of the suggested file.</param> | |
/// <param name="useEmbeddedImage">if set to <c>true</c> the embedded image must be used.</param> | |
/// <returns> | |
/// Returns path to saved resource. Path should be relative to target SVG document. | |
/// </returns> | |
public override string OnImageResourceReady(byte[] imageData, SvgImageType imageType, string suggestedFileName, ref bool useEmbeddedImage) | |
{ | |
useEmbeddedImage = this.useEmbeddedImage; | |
if (useEmbeddedImage) | |
{ | |
return suggestedFileName; | |
} | |
string fontFolder = this.outFolder; | |
if (!Directory.Exists(fontFolder)) | |
{ | |
Directory.CreateDirectory(fontFolder); | |
} | |
string fileName = fontFolder + @"\" + Path.GetFileName(suggestedFileName); | |
using (FileStream fs = new FileStream(fileName, FileMode.Create)) | |
{ | |
fs.Write(imageData, 0, imageData.Length); | |
} | |
return @"./" + this.Link + "/" + suggestedFileName; | |
} | |
#endregion | |
} | |