Wednesday, 28 December 2011

Image Resizing in ASP.NET

public static void Resize(int thumbWidth, string src, string des)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(src);
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        int thumbHeight = Convert.ToInt16(srcHeight * thumbWidth / srcWidth);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(thumbWidth, thumbHeight);

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, System.Drawing.GraphicsUnit.Pixel);

        image.Dispose();
        bmp.Save(des);

        bmp.Dispose();
     
    }

    public static void Resize(string src, string des, int thumbHeight)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(src);
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        int thumbWidth = Convert.ToInt16(srcWidth * thumbHeight / srcHeight);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(thumbWidth, thumbHeight);

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, System.Drawing.GraphicsUnit.Pixel);

        image.Dispose();
        bmp.Save(des);

        bmp.Dispose();
     
    }

No comments:

Post a Comment