We help IT Professionals succeed at work.

image resize with crop

dotsandcoms
dotsandcoms asked
on
Hi floks,

I am developing a photo gallery website and i need a function which will resize image with cropping using C#.

Please provide the way for it.

Many thanks
Comment
Watch Question

Technology Development Consultant
CERTIFIED EXPERT
Commented:
find attached code

Regards,
nishant
/// <summary>
        /// function is use for cropping and resizing images using c#
        /// </summary>
        /// <param name="ImagePath">path of image from which you want to crop a new one</param>
        /// <param name="rectangle">cropping rectanlge</param>
        /// <param name="crropedSavePath">full path of image where you want to save cropped image</param>
        /// <param name="resizeSavePath">full path of image where you want to save resized image<</param>
        /// <returns></returns>
        private bool crop_resize(string ImagePath,Rectangle rectangle,String crropedSavePath,String resizeSavePath)
        {
            try
            {
                //crop
                Size ThumbNailSize = new Size(rectangle.Width,rectangle.Height);
                System.Drawing.Image oImg = System.Drawing.Image.FromFile(ImagePath);
                System.Drawing.Image oThumbNail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
                Graphics oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality =System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                Rectangle oRectangle = rectangle;
                oGraphic.DrawImage(oImg, oRectangle);
                oThumbNail.Save(crropedSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                //resize
                ThumbNailSize = new Size(rectangle.Width,rectangle.Height);
                oImg = System.Drawing.Image.FromFile(ImagePath);
                oThumbNail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
                oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality =System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                oRectangle = new Rectangle(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
                oGraphic.DrawImage(oImg, oRectangle);
                oThumbNail.Save(crropedSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

Open in new window

Author

Commented:
good solution

Explore More ContentExplore courses, solutions, and other research materials related to this topic.