Posted By

sherazam on 06/29/16


Tagged

image png save Net to Processing an add rectangle how BY watermark crop cropping EMF shifts


Versions (?)

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


 / Published in: C#
 

URL: http://www.aspose.com/.net/imaging-component.aspx

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.

  1. // Cropping by Shifts
  2.  
  3. //[C# Code Sample]
  4.  
  5. // create an instance of Rasterization options
  6. EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions();
  7. emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke;
  8.  
  9. // create an instance of PNG options
  10. PdfOptions pdfOptions = new PdfOptions();
  11. pdfOptions.VectorRasterizationOptions = emfRasterizationOptions;
  12.  
  13. //Declare variables to store file paths for input and output images
  14. string filePath = @"TestEmfBezier.emf";
  15. string outPath = filePath + ".pdf";
  16.  
  17. //Load an existing image into an instance of EMF class
  18. using (Aspose.Imaging.FileFormats.Emf.EmfImage image = (Aspose.Imaging.FileFormats.Emf.EmfImage)Aspose.Imaging.Image.Load(filePath))
  19. {
  20. using (FileStream outputStream = new FileStream(outPath, FileMode.Create))
  21. {
  22. //Based on the shift values, apply the cropping on image
  23. //Crop method will shift the image bounds toward the center of image
  24. image.Crop(30, 40, 50, 60);
  25.  
  26. // Set height and width
  27. pdfOptions.VectorRasterizationOptions.PageWidth = image.Width;
  28. pdfOptions.VectorRasterizationOptions.PageHeight = image.Height;
  29.  
  30. //Save the results to disk
  31. image.Save(outputStream, pdfOptions);
  32. }
  33. }
  34.  
  35. //[VB.NET Code Sample]
  36.  
  37. ' create an instance of Rasterization options
  38. Dim emfRasterizationOptions As New EmfRasterizationOptions()
  39. emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke
  40.  
  41. ' create an instance of PNG options
  42. Dim pdfOptions As New PdfOptions()
  43. pdfOptions.VectorRasterizationOptions = emfRasterizationOptions
  44.  
  45. 'Declare variables to store file paths for input and output images
  46. Dim filePath As String = "TestEmfBezier.emf"
  47. Dim outPath As String = filePath & Convert.ToString(".pdf")
  48.  
  49. 'Load an existing image into an instance of EMF class
  50. Using image As Aspose.Imaging.FileFormats.Emf.EmfImage = DirectCast(Aspose.Imaging.Image.Load(filePath), Aspose.Imaging.FileFormats.Emf.EmfImage)
  51. Using outputStream As New FileStream(outPath, FileMode.Create)
  52. 'Based on the shift values, apply the cropping on image
  53. 'Crop method will shift the image bounds toward the center of image
  54. image.Crop(30, 40, 50, 60)
  55.  
  56. ' Set height and width
  57. pdfOptions.VectorRasterizationOptions.PageWidth = image.Width
  58. pdfOptions.VectorRasterizationOptions.PageHeight = image.Height
  59.  
  60. 'Save the results to disk
  61. image.Save(outputStream, pdfOptions)
  62. End Using
  63. End Using
  64.  
  65. // Cropping by Rectangle
  66.  
  67. //[C# Code Sample]
  68.  
  69. // create an instance of Rasterization options
  70. EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions();
  71. emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke;
  72.  
  73. // create an instance of PNG options
  74. PdfOptions pdfOptions = new PdfOptions();
  75. pdfOptions.VectorRasterizationOptions = emfRasterizationOptions;
  76.  
  77. //Declare variables to store file paths for input and output images
  78. string filePath = @"TestEmfExtPen.emf";
  79. string outPath = filePath + ".pdf";
  80.  
  81. //Load an existing image into an instance of EMF class
  82. using (Aspose.Imaging.FileFormats.Emf.EmfImage image = (Aspose.Imaging.FileFormats.Emf.EmfImage)Aspose.Imaging.Image.Load(filePath))
  83. {
  84. using (FileStream outputStream = new FileStream(outPath, FileMode.Create))
  85. {
  86. //Create an instance of Rectangle class with desired size
  87. //Perform the crop operation on object of Rectangle class
  88. image.Crop(new Aspose.Imaging.Rectangle(30, 50, 100, 150));
  89.  
  90. // Set height and width
  91. pdfOptions.VectorRasterizationOptions.PageWidth = image.Width;
  92. pdfOptions.VectorRasterizationOptions.PageHeight = image.Height;
  93.  
  94. //Save the results to disk
  95. image.Save(outputStream, pdfOptions);
  96. }
  97. }
  98.  
  99. //[VB.NET Code Sample]
  100.  
  101. ' create an instance of Rasterization options
  102. Dim emfRasterizationOptions As New EmfRasterizationOptions()
  103. emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke
  104.  
  105. ' create an instance of PNG options
  106. Dim pdfOptions As New PdfOptions()
  107. pdfOptions.VectorRasterizationOptions = emfRasterizationOptions
  108.  
  109. 'Declare variables to store file paths for input and output images
  110. Dim filePath As String = "TestEmfExtPen.emf"
  111. Dim outPath As String = filePath & Convert.ToString(".pdf")
  112.  
  113. 'Load an existing image into an instance of EMF class
  114. Using image As Aspose.Imaging.FileFormats.Emf.EmfImage = DirectCast(Aspose.Imaging.Image.Load(filePath), Aspose.Imaging.FileFormats.Emf.EmfImage)
  115. Using outputStream As New FileStream(outPath, FileMode.Create)
  116. 'Create an instance of Rectangle class with desired size
  117. 'Perform the crop operation on object of Rectangle class
  118. image.Crop(New Aspose.Imaging.Rectangle(30, 50, 100, 150))
  119.  
  120. ' Set height and width
  121. pdfOptions.VectorRasterizationOptions.PageWidth = image.Width
  122. pdfOptions.VectorRasterizationOptions.PageHeight = image.Height
  123.  
  124. 'Save the results to disk
  125. image.Save(outputStream, pdfOptions)
  126. End Using
  127. End Using

Report this snippet  

You need to login to post a comment.