by
8,582 17
0
5
4,264
3
Top 1% !
Famous
Easy-to-find
Specified
OpenSource
Popularity: 2149th place
Languagesmali
LicenseMIT_X11

How to Crop EMF Image using Shifts or Rectangle Approaches in .NET Applications

This technical tip explains how .NET developers can Crop an EMF Image inside their .NET applications. 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.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1311191/How-to-Crop-EMF-Image-using-Shifts-or-Re?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Enter here the actual content of the snippet. // Cropping by Shifts //[C# Code Sample] // create an instance of Rasterization options EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke; // create an instance of PNG options PdfOptions pdfOptions = new PdfOptions(); pdfOptions.VectorRasterizationOptions = emfRasterizationOptions; //Declare variables to store file paths for input and output images string filePath = @"TestEmfBezier.emf"; string outPath = filePath + ".pdf"; //Load an existing image into an instance of EMF class using (Aspose.Imaging.FileFormats.Emf.EmfImage image = (Aspose.Imaging.FileFormats.Emf.EmfImage)Aspose.Imaging.Image.Load(filePath)) { using (FileStream outputStream = new FileStream(outPath, FileMode.Create)) { //Based on the shift values, apply the cropping on image //Crop method will shift the image bounds toward the center of image image.Crop(30, 40, 50, 60); // Set height and width pdfOptions.VectorRasterizationOptions.PageWidth = image.Width; pdfOptions.VectorRasterizationOptions.PageHeight = image.Height; //Save the results to disk image.Save(outputStream, pdfOptions); } } //[VB.NET Code Sample] ' create an instance of Rasterization options Dim emfRasterizationOptions As New EmfRasterizationOptions() emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke ' create an instance of PNG options Dim pdfOptions As New PdfOptions() pdfOptions.VectorRasterizationOptions = emfRasterizationOptions 'Declare variables to store file paths for input and output images Dim filePath As String = "TestEmfBezier.emf" Dim outPath As String = filePath & Convert.ToString(".pdf") 'Load an existing image into an instance of EMF class Using image As Aspose.Imaging.FileFormats.Emf.EmfImage = DirectCast(Aspose.Imaging.Image.Load(filePath), Aspose.Imaging.FileFormats.Emf.EmfImage) Using outputStream As New FileStream(outPath, FileMode.Create) 'Based on the shift values, apply the cropping on image 'Crop method will shift the image bounds toward the center of image image.Crop(30, 40, 50, 60) ' Set height and width pdfOptions.VectorRasterizationOptions.PageWidth = image.Width pdfOptions.VectorRasterizationOptions.PageHeight = image.Height 'Save the results to disk image.Save(outputStream, pdfOptions) End Using End Using // Cropping by Rectangle //[C# Code Sample] // create an instance of Rasterization options EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke; // create an instance of PNG options PdfOptions pdfOptions = new PdfOptions(); pdfOptions.VectorRasterizationOptions = emfRasterizationOptions; //Declare variables to store file paths for input and output images string filePath = @"TestEmfExtPen.emf"; string outPath = filePath + ".pdf"; //Load an existing image into an instance of EMF class using (Aspose.Imaging.FileFormats.Emf.EmfImage image = (Aspose.Imaging.FileFormats.Emf.EmfImage)Aspose.Imaging.Image.Load(filePath)) { using (FileStream outputStream = new FileStream(outPath, FileMode.Create)) { //Create an instance of Rectangle class with desired size //Perform the crop operation on object of Rectangle class image.Crop(new Aspose.Imaging.Rectangle(30, 50, 100, 150)); // Set height and width pdfOptions.VectorRasterizationOptions.PageWidth = image.Width; pdfOptions.VectorRasterizationOptions.PageHeight = image.Height; //Save the results to disk image.Save(outputStream, pdfOptions); } } //[VB.NET Code Sample] ' create an instance of Rasterization options Dim emfRasterizationOptions As New EmfRasterizationOptions() emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke ' create an instance of PNG options Dim pdfOptions As New PdfOptions() pdfOptions.VectorRasterizationOptions = emfRasterizationOptions 'Declare variables to store file paths for input and output images Dim filePath As String = "TestEmfExtPen.emf" Dim outPath As String = filePath & Convert.ToString(".pdf") 'Load an existing image into an instance of EMF class Using image As Aspose.Imaging.FileFormats.Emf.EmfImage = DirectCast(Aspose.Imaging.Image.Load(filePath), Aspose.Imaging.FileFormats.Emf.EmfImage) Using outputStream As New FileStream(outPath, FileMode.Create) 'Create an instance of Rectangle class with desired size 'Perform the crop operation on object of Rectangle class image.Crop(New Aspose.Imaging.Rectangle(30, 50, 100, 150)) ' Set height and width pdfOptions.VectorRasterizationOptions.PageWidth = image.Width pdfOptions.VectorRasterizationOptions.PageHeight = image.Height 'Save the results to disk image.Save(outputStream, pdfOptions) End Using End Using
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus