Converting WMF and EMF to Other Image Formats

Converting EMF or WMF to PDF

Using Aspose.Imaging for .NET, developers can convert WMF metafile to PDF format. This topic explains the approach to load existing metafiles and convert it to .Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to save the image to PDF format. The following code snippet shows you how to convert WMF to PDF.

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing WMF image
using (Image image = Image.Load(dataDir + "template.emf"))
{
// Create an instance of EmfRasterizationOptions class and set different properties
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions();
emfRasterizationOptions.BackgroundColor = Color.WhiteSmoke;
emfRasterizationOptions.PageWidth = image.Width;
emfRasterizationOptions.PageHeight = image.Height;
// Create an instance of PdfOptions class and provide rasterization option
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.VectorRasterizationOptions = emfRasterizationOptions;
// Call the save method, provide output path and PdfOptions to convert the WMF file to PDF and save the output
image.Save(dataDir + "result.pdf", pdfOptions);
}
File.Delete(dataDir + "result.pdf");

Converting WMF To Webp

Using Aspose.Imaging for .NET, developers can convert WMF metafile to Webp format. This topic explains the approach to load existing metafiles and convert it to Webp format. Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to save the image to Webp format. The following code snippet shows you how to convert WMF to Webp.

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing WMF image
using (Image image = Image.Load(dataDir + "template.emf"))
{
// Calculate new Webp image height
double k = (image.Width * 1.00) / image.Height;
// Create an instance of EmfRasterizationOptions class and set different properties
EmfRasterizationOptions emfRasterization = new EmfRasterizationOptions
{
BackgroundColor = Color.WhiteSmoke,
PageWidth = 400,
PageHeight = (int)Math.Round(400 / k),
BorderX = 5,
BorderY = 10
};
// Create an instance of WebPOptions class and provide rasterization option
ImageOptionsBase imageOptions = new WebPOptions();
imageOptions.VectorRasterizationOptions = emfRasterization;
// Call the save method, provide output path and WebPOptions to convert the WMF file to Webp and save the output
image.Save(dataDir + "result.webp", imageOptions);
}
File.Delete(dataDir + "result.webp");

Converting WMF to PNG

Using Aspose.Imaging for .NET, developers can convert WMF metafile to raster format. This topic explains the approach to load existing metafiles and convert it to raster format. Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to save the image to PNG format. The following code snippet shows you how convert WMF To Raster Format.

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (Image image = Image.Load(dataDir + "template.wmf"))
{
WmfRasterizationOptions rasterizationOptions = new WmfRasterizationOptions();
rasterizationOptions.BackgroundColor = Color.WhiteSmoke;
rasterizationOptions.PageWidth = image.Width;
rasterizationOptions.PageHeight = image.Height;
image.Save(dataDir + "result.png", new PngOptions()
{
VectorRasterizationOptions = rasterizationOptions
});
}
File.Delete(dataDir + "result.png");

Converting WMF MetaFile To SVG

Using Aspose.Imaging for .NET, developers can convert WMF to SVG format. This topic explains in detail how to convert WMF to SVG format. Aspose.Imaging for .NET provides the SvgOptions class to create SVG format image. The following code snippet shows you how to convert WMF To SVG format.

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (Image image = Image.Load(dataDir + "template.wmf"))
{
WmfRasterizationOptions rasterizationOptions = new WmfRasterizationOptions();
rasterizationOptions.BackgroundColor = Color.WhiteSmoke;
rasterizationOptions.PageWidth = image.Width;
rasterizationOptions.PageHeight = image.Height;
image.Save(dataDir + "result.svg", new SvgOptions()
{
VectorRasterizationOptions = rasterizationOptions
});
}
File.Delete(dataDir + "result.svg");

Converting EMF to WMF Format

Using Aspose.Imaging for .NET, developers can convert EMF to WMF format. This topic explains in detail how to convert WMF to WMF format. The following code snippet shows you how to convert WMF To SVG format.

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load the EMF image as image and convert it to MetaImage object.
using (Aspose.Imaging.FileFormats.Emf.MetaImage image = (Aspose.Imaging.FileFormats.Emf.MetaImage)Image.Load(dataDir + "template.emf"))
{
// Convert the EMF image to WMF image by creating and passing WMF image options class object.
image.Save(dataDir + "result.wmf", new Aspose.Imaging.ImageOptions.WmfOptions()
{
VectorRasterizationOptions = new EmfRasterizationOptions()
});
}
File.Delete(dataDir + "result.wmf");

Cropping WMF file while converting to PNG

Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to crop and save the image to PNG format.

Below provided sample code demonstrate how to crop the WMF file while converting it to PNG.

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing WMF image
using (WmfImage image = (WmfImage)Image.Load(dataDir + "template.wmf"))
{
image.Crop(new Rectangle(0, 0, 100, 100));
// Create an instance of EmfRasterizationOptions class and set different properties
Aspose.Imaging.ImageOptions.WmfRasterizationOptions wmfRasterization = new Aspose.Imaging.ImageOptions.WmfRasterizationOptions
{
BackgroundColor = Color.WhiteSmoke,
PageWidth = 1000,
PageHeight = 1000
};
// Create an instance of PngOptions class and provide rasterization option
ImageOptionsBase imageOptions = new PngOptions();
imageOptions.VectorRasterizationOptions = wmfRasterization;
// Call the save method, provide output path and PngOptions to convert the cropped WMF file to PNG and save the output
image.Save(dataDir + "result.png", imageOptions);
}
File.Delete(dataDir + "result.png");

Support For saving EMF and EMF+ format to File

Using Aspose.Imaging for .NET, developers can save EMF and EMF plus format to file. This topic explains in detail how to save emf graphics files. The code snippet has been provided below.

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
var path = dataDir + "template.emf";
using (var image = (MetaImage)Image.Load(path))
{
image.Save(dataDir + "result.emf", new EmfOptions());
}
File.Delete(dataDir + "result.emf");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
var path = dataDir + "template.emf";
using (var image = (MetaImage)Image.Load(path))
{
image.Save(dataDir + "result.emf", new EmfOptions());
}
File.Delete(dataDir + "result.emf");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.ImageOptions;
using System;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(
new Rectangle(0, 0, 5000, 5000),
new Size(5000, 5000),
new Size(1000, 1000));
Font font = new Font("Arial", 10, FontStyle.Bold | FontStyle.Underline);
graphics.DrawString(font.Name + " " + font.Size + " " + font.Style.ToString(), font, Color.Brown, 10, 10);
graphics.DrawString("some text", font, Color.Brown, 10, 30);
font = new Font("Arial", 24, FontStyle.Italic | FontStyle.Strikeout);
graphics.DrawString(font.Name + " " + font.Size + " " + font.Style.ToString(), font, Color.Brown, 20, 50);
graphics.DrawString("some text", font, Color.Brown, 20, 80);
using (EmfImage image = graphics.EndRecording())
{
var path = dataDir + "result.emf";
image.Save(path, new EmfOptions());
}
File.Delete(dataDir + "result.emf");