Breaking News
Loading...
Wednesday 13 March 2013

How to upaload image with resize it, How to resize uploaded image and save

When you upload image at this time if you want to make image to be small the bellow code is use full ,here code it make image size to be 200*200 and save it to website.

/////////////////////  CODE  //////////////////////////

protected void Button1_Click(object sender, EventArgs e)
    {

        if (FileUpload1.HasFile)
        {

            string path = Server.MapPath("Photo/") + FileUpload1.PostedFile.FileName;

            // Create a bitmap of the content of the fileUpload control in memory
            Bitmap originalBMP = new Bitmap(FileUpload1.FileContent);


            // Calculate the new image dimensions
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int newWidth, newHeight;

            newWidth = 200;
            newHeight = 200;

            // Create a new bitmap which will hold the previous resized bitmap
            Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);

            // Create a graphic based on the new bitmap
            Graphics oGraphics = Graphics.FromImage(newBMP);

            // Set the properties for the new graphic file
            oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            // Draw the new graphic based on the resized bitmap
            oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

            // Save the new graphic file to the server
            newBMP.Save(path);
            //newBMP.Save(directory + "tn_" + filename);


           
            // FileUpload1.SaveAs(path);
            Image1.ImageUrl = "~/Photo/" + FileUpload1.PostedFile.FileName;
       
           

        }
    }

///////////////////////////////////////////////////////////////////////////

Download Code : UpaloadImageResize.rar

***********************************************************
See Also :
  Follow Me On Facebook
--
/\/ir@\/  <(.'.)>

0 comments:

Post a Comment

Thanks for comment

 
Toggle Footer