Image Upload & Resize in asp.net with c#.net

         Image Upload & Resize in asp.net
Task: Upload Image & Resize it proportionally.
Description: Now days we require upload product images in a E-Commerce Website & we want to resize it also. So we can resize image when uploaded by using admin panel.

Here I use below code to resize image with its proportionally. So it will not stretch. This code is useful for jpg and png images.

using System.Drawing;
 protected void button_upload_image(object sender, EventArgs e)
    {
       if (FileUpload1.PostedFile.ContentLength != 0)
       {
           string temp = Server.MapPath(@"images\");
           string path = temp + "hemant.jpg";
           temp = temp + "temp.jpg";
           FileUpload1.SaveAs(temp);
           setimagesize(temp, path, 800, 600,400,300);
        } 
    }



// Call this function for resizing ( compress height and width proportionally )
// destination and imagepath must be differ
 protected void setimagesize(string imagepath, string destination, int maxwidth, int maxheight,int minwid,int minhight)
    {
        string path = imagepath.ToString();
        string dest = destination.ToString();

        System.Drawing.Image img1 = System.Drawing.Image.FromFile(path); // source of large image
        int owd = Convert.ToInt32(img1.Width);
        int oht = Convert.ToInt32(img1.Height);
        //       Compress Large Image Height/Width to according our canvas max(800*600)
        int nwd, nht;
        nwd = owd;
        nht = oht;
        if (owd > oht)
        {
            if (owd > maxwidth)                                // For Maximum width
            {
                nwd = maxwidth;
                nht = oht * maxwidth / owd;
            }
            else if (owd < minwid)                      // For minimum width
            {
                nwd = minwid;
                nht = oht * minwid / owd;
            }
        }
        else
        {
            if (oht > maxheight)                                // For Maximum height
            {
                nht = maxheight;
                nwd = owd * maxheight / oht;
            }
            else if (oht < minhight)                    //  For Minimum width
            {
                nht = minhight;
                nwd = owd * minhight / oht;
            }
        }

        ///////////////////////////
        //     Saving Compress Image
        ///////////////////////////////////////
        Bitmap bmp = new Bitmap(nwd, nht);
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, nwd, nht);
        gr.DrawImage(img1, rectDestination, 0, 0, owd, oht, GraphicsUnit.Pixel);
        bmp.Save(dest, img1.RawFormat); //image size reduce by using this code changing by    
        bmp.Save(dest);
        bmp.Dispose();
       gr.Dispose();
        img1.Dispose();
    }

4 comments:

  1. India eData Solutions provides Ecommerce Image Processing Services, Ecommerce Products Updating Services, 3Ecommerce Image Processing Services for E-commerce Industry at lowest rates in all over world. we have expert team for Outsource 3dCart product listing Project.

    ReplyDelete
  2. Get the Best Bulk Product Upload Services From Us With the drastic facelift that the retail sector has seen in the recent past, the internet gas become the hub for most of the small, medium as well as large scale businesses that are involved in the same.The ecommerce has seen a monumental growth prospective over the last couple of years and the trend is expected to remain the same for the foreseeable future as well. In this regard, what concerns the businesses and the online retail outlet is the data entry aspect. Tech data entry India provides a number of products and services to cater to the Amazon Bulk Product Listing Services.

    ReplyDelete