:::: MENU ::::
Posts tagged with: mvc

Run Time Crop Images with asp.net mvc generic handler

some time  we ask end-user to upload the image and user can upload any size of image while image dimension does not fit our requirement i.e height and width does not have suite able aspect ratio so solution is to resize image forcefully or ask user to crop the image by himself. there are number of Client side image-cropping solution but here i would discuss server side approach in order to crop the image which can be combined with any client side Cropping solution.

generic handler in asp.net are best to return dynamic content like images and files. here i would create a generic handler using mvc 5 (any Mvc version can be used even web form asp.net with little modification ) in order to Crop images Run time. our generic handler will take image path and its dimension like height width to crop it and return cropped image to browser.
Here is the Code of Generic Handler

 


now we need to define a route in mvc RouteConfig Class so we can access the generic handler


Make sure runAllManagedModulesForAllRequests=true in you web.cofing file

and finally we should place images on our Images Directory which resides at root Directory

http://localhost/images

final out put will be look like this

Image without Crop

stream flowing through forest

Cropped Image

here we trigger generic handler by following below URL Pattern in order to crop the imag

 http:/localhost/cropImages/x-y-h-w-image.jpg

where point(x,y) are left ,top positions to start cropping and point(h,w) are
height and width of cropped images.
we will get the below result by following this URL

http://localhost/cropImages/150-150-400-400-nature.jpg
150-150-400-400-nature

http://localhost/cropImages/150-150-400-400-nature.jpg

You can download the complete solution RunTime-Image-Cropper

Please follow and like us: