Image Resize with auto-crop

Task: Image resize, auto-crop and auto scale.

Description: Here is sample code for image resize with auto-crop while maintaining its ratio. below function we need to pass image path, maximum width and height for resizing. And then below code will resized to given width & height & crop the remaining part which not covered under given measurement. 



public void drawimage(string ThumbnailPath, int maxwidth, int maxheight)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(ThumbnailPath);
       
        if (maxheight == 0 || maxwidth == 0)
        {
            maxwidth = image.Width;
            maxheight = image.Height;
        }
        Size ThumbNailSize = setimagesize(maxwidth, maxheight, image.Width, image.Height);

        System.Drawing.Image ImgThnail;
        if (image.Width < maxwidth || image.Height < maxheight)
        {
            ImgThnail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
        }
        else
        {
            ImgThnail = new Bitmap(maxwidth, maxheight);
        }
       
        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(ImgThnail);
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphics.CompositingQuality = CompositingQuality.HighQuality;

        if (image.Width < maxwidth || image.Height < maxheight)
        {
            graphics.DrawImage(image, 0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
        }
        else
        {
            int xcord = 0;
            int ycord = 0;
            {
                xcord = (ThumbNailSize.Width - maxwidth) / 2;
                ycord = (ThumbNailSize.Height - maxheight) / 2;
            }

            graphics.DrawImage(image, -xcord, -ycord, ThumbNailSize.Width, ThumbNailSize.Height);

        }


        ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
        EncoderParameters encoderParameters;
        encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

        string sExt = System.IO.Path.GetExtension(ThumbnailPath);

        if (sExt.ToString() == ".png")
        {
            Response.ContentType = "image/png";
            ImgThnail.Save(Response.OutputStream, image.RawFormat);
        }
        else
        {
            Response.ContentType = "image/jpeg";
            ImgThnail.Save(Response.OutputStream, info[1], encoderParameters);
        }
        image.Dispose();

    }




    // below code is my old one for calculating height and width with maintain image ratio.

public Size setimagesize(int maxwidth, int maxheight, int OriginalWidth, int OriginalHeight)
    {
        Size NewSize = new Size();
        if (OriginalHeight < maxheight && OriginalWidth < maxwidth)
        {
            NewSize = new Size(OriginalWidth, OriginalHeight);
            return NewSize;
        }
        int sNewWidth = OriginalWidth;
        int sNewHeight = OriginalHeight;
        int tempheight = 0;
        int tempwidht = 0;

        if (OriginalWidth >= OriginalHeight)
        {
            if (OriginalWidth >= maxwidth)
            {
                sNewWidth = maxwidth;
                sNewHeight = OriginalHeight * maxwidth / OriginalWidth;
            }
            if (sNewHeight < maxheight)
            {
                tempheight = sNewHeight;
                sNewHeight = maxheight;
                sNewWidth = sNewWidth * maxheight / tempheight;
            }
        }
        else
        {
            if (OriginalHeight >= maxheight)
            {
                sNewHeight = maxheight;
                sNewWidth = OriginalWidth * maxheight / OriginalHeight;
            }
            if (sNewWidth < maxwidth)
            {
                tempwidht = sNewWidth;
                sNewWidth = maxwidth;
                sNewHeight = sNewHeight * maxwidth / tempwidht;
            }
        }
        NewSize = new Size(sNewWidth, sNewHeight);
        return NewSize;
    }

1 comment:

  1. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information about the web design and web development.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Asp.net developers
    .Net developer freelance

    ReplyDelete