article

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

Email
Submitted on: 6/29/2016 7:38:12 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 1972
 
     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. The EmfImage class provides an overloaded version of the Crop method that accepts 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the Crop method moves the image boundaries toward the center of the image while discarding the outer portion.

 
				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. The EmfImage class provides an overloaded version of the Crop method that accepts 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the Crop method moves the image boundaries toward the center of the image while discarding the outer portion. The EmfImage class provides another overloaded version of the 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.
// 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
Overview: Aspose.Imaging for .NET
Aspose.Imaging for .NET is an image processing & manipulation component that allows developers to create, edit, draw or convert images in their .NET application. It allows developers to convert image files to PSD, BMP, JPEG, PNG, TIFF and GIF formats. Moreover a set of pens, brushes and fonts can be used to draw images or add new elements & text to existing images. Aspose.Imaging for .NET works well with both web & windows applications. Moreover, it adds the support for Silverlight platform.


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.