With Aspose.Total for .NET, converting CGM files to WMF (Windows Metafile) images within any .NET application is a breeze. This can be accomplished in just two simple steps using Aspose.PDF for .NET and Aspose.Imaging for .NET Image Processing API.
Firstly, using Aspose.PDF for .NET, you can export CGM files to JPEG format. Aspose.PDF for .NET provides a robust API for working with PDF documents, ensuring a seamless CGM to JPEG conversion process.
Next, by leveraging Aspose.Imaging for .NET’s Image Processing API, you can effortlessly convert the JPEG image obtained in the previous step to WMF format. Aspose.Imaging for .NET offers a comprehensive set of tools and features for image manipulation, facilitating the conversion of JPEG to WMF.
Convert CGM file to WMF via .NET
- Open CGM file using Document class
- Initialize JpegDevice class object and render CGM to JPEG by using Process method
- Load JPEG file by using Image class
- Save the document to WMF format using Save method
Conversion Requirements
Install from command line as nuget install Aspose.Total
or install directly from Package Manager Console of Visual Studio.
Two Aspose.Total for .NET child APIs, Aspose.PDF for .NET and Aspose.Imaging for .NET will be used.
Alternatively, get the offline MSI installer or DLLs in a ZIP file from downloads .
// supports PDF, CGM, EPUB, TeX, PCL, PS, XPS, MD, MHTML, XSLFO, HTML file formats | |
// load PDF with an instance of Document | |
var document = new Document("input.pdf"); | |
// create an object of jpegDevice | |
var renderer = new JpegDevice(); | |
// convert a particular page and save the image in JPEG format | |
renderer.Process(document.Pages[1], "output.jpeg"); | |
// load JPEG file | |
var image = Image.Load("output.jpeg"); | |
// supports Dicom, Jpeg2000, Apng, Psd, Dxf, Wmf, Emz, Wmz, Tga, Svgz file formats | |
// save JPEG to PSD file format | |
image.Save("output.psd", new PsdOptions()); |
Convert CGM File to WMF in a Single File via C#
Using the API, you can also convert CGM file to WMF to a single image file. In order to convert all pages, you can first render your CGM document to one TIFF file and after that you can export TIFF file to WMF. You can open the input file using Document class and create Resolution, TiffSettings, & TIFF device objects. You can get a single TIFF image using Process method of TiffDevice class. Finally, you can load TIFF file using Image class and save it to WMF format using Save method.
// supports PDF, CGM, EPUB, TeX, PCL, PS, XPS, MD, MHTML, XSLFO, HTML file formats | |
// Open PDF document | |
Document pdfDocument = new Document("input.pdf"); | |
// Create Resolution object | |
Resolution resolution = new Resolution(300); | |
// Create TiffSettings object | |
TiffSettings tiffSettings = new TiffSettings | |
{ | |
Compression = CompressionType.None, | |
Depth = ColorDepth.Default, | |
Shape = ShapeType.Landscape, | |
SkipBlankPages = false | |
}; | |
// Create TIFF device | |
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings); | |
// Convert a particular page and save the image to stream | |
tiffDevice.Process("output.tif"); | |
// load TIFF file | |
var image = Image.Load("output.tif"); | |
// supports Dicom, Jpeg2000, Apng, Psd, Dxf, Wmf, Emz, Wmz, Tga, Svgz file formats | |
// save TIFF to PSD file format | |
image.Save("output.psd", new PsdOptions()); |
Convert CGM to WMF With Watermark via C#
Using the API, you can also convert CGM file to WMF with watermark in your WMF document. In order to add a watermark, you can first render your CGM document to JPEG and add a watermark in it. To demonstrate the operation, you can load your converted JPEG image, add transformations using an object of Matrix class and draw a string as the watermark on the image surface using the Graphics class’ DrawString method. After adding the watermark in it, you can save the JPEG as WMF format. Below is a code example that demonstrates how to add a diagonal watermark to your document.
// supports PDF, CGM, EPUB, TeX, PCL, PS, XPS, MD, MHTML, XSLFO, HTML file formats | |
// load PDF with an instance of Document | |
var document = new Document("input.pdf"); | |
// create an object of jpegDevice | |
var renderer = new JpegDevice(); | |
// convert a particular page and save the image in JPEG format | |
renderer.Process(document.Pages[1], "output.jpeg"); | |
// load an existing JPEG image | |
Image image = Image.Load("output.jpeg"); | |
// declare a String object with Watermark Text | |
string theString = "45 Degree Rotated Text"; | |
// create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size | |
Graphics graphics = new Graphics(image); | |
SizeF sz = graphics.Image.Size; | |
// create an instance of Font, initialize it with Font Face, Size and Style | |
Font font = new Font("Times New Roman", 20, FontStyle.Bold); | |
// create an instance of SolidBrush and set its various properties | |
SolidBrush brush = new SolidBrush(); | |
brush.Color = Color.Red; | |
brush.Opacity = 0; | |
// initialize an object of StringFormat class and set its various properties | |
StringFormat format = new StringFormat(); | |
format.Alignment = StringAlignment.Center; | |
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; | |
// create an object of Matrix class for transformation | |
Matrix matrix = new Matrix(); | |
// first a translation then a rotation | |
matrix.Translate(sz.Width / 2, sz.Height / 2); | |
matrix.Rotate(-45.0f); | |
// set the Transformation through Matrix | |
graphics.Transform = matrix; | |
// draw the string on Image Save output to disk | |
graphics.DrawString(theString, font, brush, 0, 0, format); | |
// supports Dicom, Jpeg2000, Apng, Psd, Dxf, Wmf, Emz, Wmz, Tga, Svgz file formats | |
// save JPEG to PSD file format with watermark in it | |
image.Save("output.psd", new PsdOptions()); |
Convert & Rotate CGM File to WMF via C#
Using the API, you can also rotate the output WMF image as per your needs. The Image.RotateFlip method can be used to rotate the image by 90/180/270-degrees and flip the image horizontally or vertically. You can specify the type of rotation and flip to apply to the image. In order to rotate and flip the image you can load the converted JPEG image using the factory method exposed by Image class and call the Image.RotateFlip method while specifying the appropriate RotateFlipType .
// supports PDF, CGM, EPUB, TeX, PCL, PS, XPS, MD, MHTML, XSLFO, HTML file formats | |
// load PDF with an instance of Document | |
var document = new Document("input.pdf"); | |
// create an object of jpegDevice | |
var renderer = new JpegDevice(); | |
// convert a particular page and save the image in JPEG format | |
renderer.Process(document.Pages[1], "output.jpeg"); | |
// load JPEG file | |
var image = Image.Load("output.jpeg"); | |
// rotate the image | |
image.RotateFlip(RotateFlipType.Rotate270FlipNone); | |
// supports Dicom, Jpeg2000, Apng, Psd, Dxf, Wmf, Emz, Wmz, Tga, Svgz file formats | |
// save JPEG to PSD file format | |
image.Save("output.psd", new PsdOptions()); |