Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: How to crop an Image

From Wiki

Jump to: navigation, search


Summary: An example function to crop an existing image based on parameterised points

  1. Private Function CropImage(ByVal OriginalImage As Bitmap, ByVal TopLeft As Point, ByVal BottomRight As Point) As Bitmap
  2.     Dim btmCropped As New Bitmap((BottomRight.Y - TopLeft.Y), (BottomRight.X - TopLeft.X))
  3.     Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)
  4.  
  5.     grpOriginal.DrawImage(OriginalImage, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), _
  6.         TopLeft.X, TopLeft.Y, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)
  7.     grpOriginal.Dispose()
  8.  
  9.     Return btmCropped
  10. End Function

Example usage

  1. Dim bmpImage As New Bitmap("c:\point.jpg")
  2. bmpImage = CropImage(bmpImage, New Point(0, 0), New Point(100, 100))


This Hack is part of the ASP.NET Hacks collection

456 Rating: 2.9/5 (14 votes cast)