DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Cropping EMF Images With Shift or Rectangle Approaches in .NET

This article explains how .NET developers can crop an EMF image inside their .NET applications.

Sheraz Khan user avatar by
Sheraz Khan
·
Jul. 03, 16 · Tutorial
Like (1)
Save
Tweet
Share
3.12K Views

Join the DZone community and get the full member experience.

Join For Free

This article 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 images: by shifts and by rectangles. The EmfImage class provides an overloaded version of the Crop method that accepts 4 integer values denoting left, right, top, and 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

In C#:

//[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);
  }
}


Or, in VB.NET:

//[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

In C#:

//[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);
    }
}


Or, in VB.NET:

//[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 and text to existing images. Aspose.Imaging for .NET works well with both web and windows applications. Also, it provides support for the Silverlight platform.

Eclipse Modeling Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Apache Kafka vs. Memphis.dev
  • Memory Debugging: A Deep Level of Insight
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: