Manipulating WMF Files
Create WMF MetaFile Image
Using Aspose.Imaging for .NET, developers can create WMF metafile image. This topic explains in detail that how WMF metafile image can be created. Aspose.Imaging for .NET provides the WmfRecorderGraphics2D class to create WMF metafile. The following code snippet shows you how to create WMF Metafile Image.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
WmfRecorderGraphics2D graphics = new WmfRecorderGraphics2D(new Rectangle(0, 0, 100, 100), 96); | |
// Define background color | |
graphics.BackgroundColor = Color.WhiteSmoke; | |
// Init Create an instance of Imaging Pen class, Brush class and mention its color. | |
Pen pen = new Pen(Color.Blue); | |
Brush brush = new SolidBrush(Color.YellowGreen); | |
// Polygon Fill polygon and then Draw a polygon | |
graphics.FillPolygon(brush, new[] { new Point(2, 2), new Point(20, 20), new Point(20, 2) }); | |
graphics.DrawPolygon(pen, new[] { new Point(2, 2), new Point(20, 20), new Point(20, 2) }); | |
brush = new HatchBrush { HatchStyle = HatchStyle.Cross, BackgroundColor = Color.White, ForegroundColor = Color.Silver }; | |
// Fill ellipse and Draw an ellipse | |
graphics.FillEllipse(brush, new Rectangle(25, 2, 20, 20)); | |
graphics.DrawEllipse(pen, new Rectangle(25, 2, 20, 20)); | |
// Arc Define pen style by setting DashStyle value, Set color of the pen | |
pen.DashStyle = DashStyle.Dot; | |
pen.Color = Color.Black; | |
// Draw an Arc by calling DrawArc method and set CubicBezier | |
graphics.DrawArc(pen, new Rectangle(50, 2, 20, 20), 0, 180); | |
pen.DashStyle = DashStyle.Solid; | |
pen.Color = Color.Red; | |
// Draw an CubicBezier | |
graphics.DrawCubicBezier(pen, new Point(10, 25), new Point(20, 50), new Point(30, 50), new Point(40, 25)); | |
// Image Create an Instance of Image class. | |
using (Image image = Image.Load(dataDir + @"template.bmp")) | |
{ | |
// Cast the instance of image class to RasterImage. | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage != null) | |
{ | |
// Draw an image by calling DrawImage method and passing parameters rasterimage and point. | |
graphics.DrawImage(rasterImage, new Point(50, 50)); | |
} | |
} | |
// Line Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen. | |
graphics.DrawLine(pen, new Point(2, 98), new Point(2, 50)); | |
// Pie Define settings of the brush object. | |
brush = new SolidBrush(Color.Green); | |
pen.Color = Color.DarkGoldenrod; | |
// Fill pie by calling FillPie method and passing parameters brush and an instance of Imaging Rectangle class. | |
graphics.FillPie(brush, new Rectangle(2, 38, 20, 20), 0, 45); | |
// Draw pie by calling DrawPie method and passing parameters pen and an instance of Imaging Rectangle class. | |
graphics.DrawPie(pen, new Rectangle(2, 38, 20, 20), 0, 45); | |
pen.Color = Color.AliceBlue; | |
// Polyline Draw Polyline by calling DrawPolyline method and passing parameters pen and points. | |
graphics.DrawPolyline(pen, new[] { new Point(50, 40), new Point(75, 40), new Point(75, 45), new Point(50, 45) }); | |
// For having Strings Create an instance of Font class. | |
Font font = new Font("Arial", 16); | |
// Draw String by calling DrawString method and passing parameters string to display, color and X & Y coordinates. | |
graphics.DrawString("Aspose", font, Color.Blue, 25, 75); | |
// Call end recording of graphics object and save WMF image by calling Save method. | |
using (WmfImage image = graphics.EndRecording()) | |
{ | |
image.Save(dataDir + "result.wmf"); | |
} | |
File.Delete(dataDir + "result.wmf"); |
Create EMF MetaFile Image
Using Aspose.Imaging for .NET, developers can create EMF metafile image. This topic explains in detail that how EMF metafile image can be created. This topic also covers the test to convert the newly generated EMF metafile image to PDF format. Aspose.Imaging for .NET provides the EmfRecorderGraphics2D class to create EMF metafile. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
// EmfRecorderGraphics2D class provides you the frame or canvas to draw shapes on it. | |
EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(new Rectangle(0, 0, 1000, 1000), new Size(1000, 1000), new Size(100, 100)); | |
{ | |
// Create an instance of Imaging Pen class and mention its color. | |
Pen pen = new Pen(Color.Bisque); | |
// Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen. | |
graphics.DrawLine(pen, 1, 1, 50, 50); | |
// Reset the Pen color Specify the end style of the line. | |
pen = new Pen(Color.BlueViolet, 3); | |
pen.EndCap = LineCap.Round; | |
// Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen and end style of line. | |
graphics.DrawLine(pen, 15, 5, 50, 60); | |
// Specify the end style of the line. | |
pen.EndCap = LineCap.Square; | |
graphics.DrawLine(pen, 5, 10, 50, 10); | |
pen.EndCap = LineCap.Flat; | |
// Draw a line by calling DrawLine method and passing parameters. | |
graphics.DrawLine(pen, new Point(5, 20), new Point(50, 20)); | |
// Create an instance of HatchBrush class to define rectanglurar brush with with different settings. | |
HatchBrush hatchBrush = new HatchBrush | |
{ | |
BackgroundColor = Color.AliceBlue, | |
ForegroundColor = Color.Red, | |
HatchStyle = HatchStyle.Cross | |
}; | |
// Draw a line by calling DrawLine method and passing parameters. | |
pen = new Pen(hatchBrush, 7); | |
graphics.DrawRectangle(pen, 50, 50, 20, 30); | |
// Draw a line by calling DrawLine method and passing parameters with different mode. | |
graphics.BackgroundMode = EmfBackgroundMode.OPAQUE; | |
graphics.DrawLine(pen, 80, 50, 80, 80); | |
// Draw a polygon by calling DrawPolygon method and passing parameters with line join setting/style. | |
pen = new Pen(new SolidBrush(Color.Aqua), 3); | |
pen.LineJoin = LineJoin.MiterClipped; | |
graphics.DrawPolygon(pen, new[] | |
{ | |
new Point(10, 20), | |
new Point(12, 45), | |
new Point(22, 48), | |
new Point(48, 36), | |
new Point(30, 55) | |
}); | |
// Draw a rectangle by calling DrawRectangle method. | |
pen.LineJoin = LineJoin.Bevel; | |
graphics.DrawRectangle(pen, 50, 10, 10, 5); | |
pen.LineJoin = LineJoin.Round; | |
graphics.DrawRectangle(pen, 65, 10, 10, 5); | |
pen.LineJoin = LineJoin.Miter; | |
graphics.DrawRectangle(pen, 80, 10, 10, 5); | |
// Call EndRecording method to produce the final shape. EndRecording method will return the final shape as EmfImage. So create an instance of EmfImage class and initialize it with EmfImage returned by EndRecording method. | |
using (EmfImage image = graphics.EndRecording()) | |
{ | |
// Create an instance of PdfOptions class. | |
PdfOptions options = new PdfOptions(); | |
// Create an instance of EmfRasterizationOptions class and define different settings. | |
EmfRasterizationOptions rasterizationOptions = new EmfRasterizationOptions(); | |
rasterizationOptions.PageSize = image.Size; | |
options.VectorRasterizationOptions = rasterizationOptions; | |
string outPath = dataDir + "result.pdf"; | |
image.Save(outPath, options); | |
} | |
} | |
File.Delete(dataDir + "result.pdf"); |
Resize WMF file while converting to PNG
Using Aspose.Imaging for .NET, developers can resize the WMF metafile while converting it to raster format. This topic explains the approach to load existing metafiles, resize it and convert it to raster format. Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to resize and save the image to PNG format. The following code snippet shows you how to resize the WMF file while converting it to PNG.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
// Load an existing WMF image | |
using (Image image = Image.Load(dataDir + "template.wmf")) | |
{ | |
// Call the resize method of Image class and width,height values and Calculate new PNG image height | |
image.Resize(100, 100); | |
double k = (image.Width * 1.00) / image.Height; | |
// Create an instance of EmfRasterizationOptions class and set different properties | |
WmfRasterizationOptions emfRasterization = new WmfRasterizationOptions | |
{ | |
BackgroundColor = Color.WhiteSmoke, | |
PageWidth = 100, | |
PageHeight = (int)Math.Round(100 / k), | |
BorderX = 5, | |
BorderY = 10, | |
PageSize = image.Size | |
}; | |
// Create an instance of PngOptions class and provide rasterization option | |
PngOptions imageOptions = new PngOptions(); | |
imageOptions.VectorRasterizationOptions = emfRasterization; | |
// Call the save method, provide output path and PngOptions to convert the WMF file to PNG and save the output | |
image.Save(dataDir + "result.png", imageOptions); | |
} | |
File.Delete(dataDir + "result.png"); |
Converting EMF to WMF
Using Aspose.Imaging for .NET, developers can convert EMF to WMF metafile format. This topic explains the approach to load existing EMF metafiles and convert it to WMF format. Aspose.Imaging for .NET provides the Image class to load EMF files and same can be used to save the image to WMF format. The following code snippet shows you how to convert EMF to WMF.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
// Load an existing EMF file as Image. | |
using (Image image = Image.Load(dataDir + "template.emf")) | |
{ | |
VectorRasterizationOptions rasterizationOptions = new EmfRasterizationOptions(); | |
rasterizationOptions.PageWidth = image.Width; | |
rasterizationOptions.PageHeight = image.Height; | |
WmfOptions exportOptions = new WmfOptions(); | |
exportOptions.VectorRasterizationOptions = rasterizationOptions; | |
// Call the Save method of Image class & Pass instance of WmfOptions class to Save method. | |
image.Save(dataDir + "result.wmf", exportOptions); | |
} | |
File.Delete(dataDir + "result.wmf"); |
Exporting Text as Shape While Converting EMF MetaFile
Using Aspose.Imaging for .NET, developers can get text as shapes while converting EMF to SVG format. This topic explains in detail how to convert text into shape while converting EMF to SVG format. Aspose.Imaging for .NET provides the TextAsShapes property to get text as shape while converting EMF metafile. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
using (Image image = Image.Load(dataDir + "template.emf")) | |
{ | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.White; | |
emfRasterizationOptions.PageWidth = image.Width; | |
emfRasterizationOptions.PageHeight = image.Height; | |
image.Save(dataDir + "result.svg", new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
TextAsShapes = true | |
}); | |
image.Save(dataDir + "result2.svg", new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
TextAsShapes = false | |
}); | |
} | |
File.Delete(dataDir + "result.svg"); | |
File.Delete(dataDir + "result2.svg"); |
Get Last Modified Date of A Raster Image
This article demonstrates the usage of Aspose.Imaging for .NET to get last modified date of an image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal.
Get Last Modified Date
Aspose.Imaging for .NET has exposed the GetModifyDate method of Image class to get the last modified date of an image from its metadata. GetModifyDate method needs a Boolean value to get the modified date accordingly. The steps to get last modified date are as simple as below:
- Load an image using the factory method Load exposed by Image class.
- Convert the image into RasterImage.
- Call the RasterImage.GetModifyDate method by passing a Boolean true or false value.
The following code snippet shows you how to get last modified date of the image.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
using (RasterImage image = (RasterImage)Image.Load(dataDir + "template.jpg")) | |
{ | |
// Gets the date from [FileInfo] | |
string modifyDate = image.GetModifyDate(true).ToString(); | |
Console.WriteLine("Last modify date using [FileInfo]: {0}", modifyDate); | |
// Gets the date from XMP metadata of [FileInfo] as long as it's not default case | |
modifyDate = image.GetModifyDate(false).ToString(); | |
Console.WriteLine("Last modify date using info from [FileInfo] and XMP metadata: {0}", modifyDate); | |
} |
Crop WMF 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 for to cut out some portion of an image to increase the focus on a particular area. The WmfImage class provides 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 to Crop any image by Rectangle.
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Core.VectorPaths; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Emf.Emf.Consts; | |
using Aspose.Imaging.FileFormats.Emf.Graphics; | |
using Aspose.Imaging.FileFormats.Gif; | |
using Aspose.Imaging.FileFormats.Gif.Blocks; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.FileFormats.Tiff.PathResources; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.FileFormats.Wmf; | |
using Aspose.Imaging.FileFormats.Wmf.Graphics; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string fileName = Path.Combine(dataDir, "template.tiff"); | |
using (WmfImage image = Image.Load(dataDir + "template.wmf") as WmfImage) | |
{ | |
image.Crop(new Rectangle(10, 10, 20, 20)); | |
Console.WriteLine(image.Width); | |
Console.WriteLine(image.Height); | |
image.Save(dataDir + "result.wmf"); | |
} | |
File.Delete(dataDir + "result.wmf"); |