Saturday, 20 July 2013

IMAGE RESIZE CODE

In many  website we need to upload images with specific height and width without loosing images quality .Today we will see how to resize image in asp.net C#


1 Step: open visual studio and add class file in it.

using System.Drawing;
using System.Drawing.Drawing2D;

using System.IO;


public class Imageresize
{
// It will resize image horizontal you need to pass height and width
 public static void ResizeImageHorizon(Stream fromStream, Stream toStream, double Height, double Width)
    {
        try
        {
            double newHeight = 0;

            double newWidth = 0;
            double ratio = (Width / Height);
            using (var image = System.Drawing.Image.FromStream(fromStream))
            {

                //Ratio of 1.5 for width 900 height = 600 
                if (image.Width >= Width || image.Height >= Height)
                {
                    if (ratio < Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height))
                    {
                        newWidth = Width;
                        newHeight = Convert.ToDouble(image.Height) / (Convert.ToDouble(image.Width) / Width);

                    }
                    else if (ratio >= Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height))
                    {
                        newHeight = Height;
                        newWidth = Convert.ToDouble(image.Width) / (Convert.ToDouble(image.Height) / Height);
                    }
                }
                else
                {

                    newWidth = (int)(image.Width);

                    newHeight = (int)(image.Height);

                }



                var imageRectangle = new Rectangle(0, 0, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));

                using (var thumbnailBitmap = new Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight)))
                {

                    using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
                    {

                        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;

                        thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;

                        thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                        thumbnailGraph.DrawImage(image, imageRectangle);

                        thumbnailBitmap.Save(toStream, image.RawFormat);


                    }

                }

            }

        }

        catch { }
    }

// It will resize image Vertically you need to pass height and width

 public static void ResizeImageVertical(Stream fromStream, Stream toStream, double Height, double Width)
    {
        double newHeight = 0;

        double newWidth = 0;

        double ratio = (Width / Height);

        using (var image = System.Drawing.Image.FromStream(fromStream))
        {
            //0.66 for width 90 height = 135 
            if (image.Width >= Width || image.Height >= Height)
            {
                if (ratio < Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height))
                {
                    newWidth = Width;
                    newHeight = Convert.ToDouble(image.Height) / (Convert.ToDouble(image.Width) / Width);

                }
                else if (ratio >= Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height))
                {
                    newHeight = Height;
                    newWidth = Convert.ToDouble(image.Width) / (Convert.ToDouble(image.Height) / Height);
                }
            }
            else
            {

                newWidth = (int)(image.Width);

                newHeight = (int)(image.Height);

            }




            var imageRectangle = new Rectangle(0, 0, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));

            using (var thumbnailBitmap = new Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight)))
            {

                using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
                {

                    thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;

                    thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;

                    thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    thumbnailGraph.DrawImage(image, imageRectangle);

                    thumbnailBitmap.Save(toStream, image.RawFormat);


                }

            }

        }

    }

}

NOW ON BUTTON CLICK
 protected void btnSubmit_Click(object sender, EventArgs e)

    {

 if (flImageUpload.HasFile)
                    {
//CHECK IMAGE TYPE
                        if ((Path.GetExtension(flImageUpload.FileName).ToLower() == ".jpg" || Path.GetExtension(flImageUpload.FileName).ToLower() == ".png"
                        || Path.GetExtension(flImageUpload.FileName).ToLower() == ".jpeg" || Path.GetExtension(flImageUpload.FileName).ToLower() == ".gif"))
                        {

                            try
                            {
                                string _filename = DateTime.Now.Ticks.ToString() flImageUpload.FileName.ToString();

                                flImageUpload.SaveAs(Server.MapPath("~/Images/" + _filename));
                                var path = Server.MapPath("~/Images/" + _filename);

                                //here set path for Thumbnail Image 


                                var thumbPath1 = Server.MapPath("~/Images/Thumb/" + _filename);
                                using (var file = File.OpenRead(path))
                                using (var thumbFile = File.Create(thumbPath1))

                                    //Imageresize.ThumbnailImage(file, thumbFile);
                                     //here You need to pass height and width 
                                    Imageresize.ResizeImageHorizon(file, thumbFile, 138, 138);

                                 //here set path for ResizeImage

                                var thumbPath = Server.MapPath("~/Images/ResizeImage/" + _filename);
                                var path2 = Server.MapPath("~/Images/" + _filename);
                                using (var file = File.OpenRead(path2))
                                using (var thumbFile = File.Create(thumbPath))

                             //here You need to pass height and width
                                    Imageresize.ResizeImageHorizon(file, thumbFile, 400, 380);
                                  

                                if (File.Exists(path2))
                                {
                                    File.Delete(path2);

                                }

    }

No comments:

Post a Comment