Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Aspose.Imaging .NET API allows to easy manipulate your wmf images or photos in your .NET application or Web service.

You can:

  • Convert gif image to webp;
  • Create webp image;
  • Export Image to WebP;
  • Export WebP to Other Image Formats;
  • Extract Frames From WebP Image.

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

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");
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");
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");
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");
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");
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);
}
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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment