Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to resize an uploaded image in Asp.net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 693
    Comment on it

    "How to resize an uploaded image in Asp.net"

        In this article I have explained that how to resize the uploaded image and then save it to a location.

    Getting Started:

    Step 1: Place a FileUpload control, a Button on the Web Form.

    Step 2: The user will first upload the file using FileUpload control and will then click the save button. Therefore generate the click event for the button.

    Step 3: Onclick event in the code behind write the following code:

        Bitmap image = ResizeImage(flpBrowseUserImage.PostedFile.InputStream, 120, 80);
    
                    string filename = flpBrowseUserImage.FileName.ToString();
                    string extension = Path.GetExtension(filename);
                    filename = filename.Replace(extension, DateTime.Now.ToString("yyyyMMddHHmmss"));
                    filename = filename + extension;
                    filename = filename.Replace(' ', '_').ToString();
                    image.Save(Server.MapPath("~/Profile_Images/" + filename), ImageFormat.Jpeg);
    

       In this code block we have called a method ResizeImage()(Actually responcible for resizing the image) which I have discussed in next step.This method returns the Bitmap image after resizing it as per the passed width and height which is then saved to a location after making some changes to its name (as per the requirement).

    Note: Take the reference of Dll "System.Drawing.Imaging"

    Step 4: As you can see that we are using ResizeImage() method, so make a method for it as follows:

    private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
            {
                Bitmap originalImage = new Bitmap(streamImage);
                int newWidth = maxWidth;
                int newHeight = maxHeight;
                return new Bitmap(originalImage, newWidth, newHeight);
            }
    

       This method takes the input image stream, width and height and will return the bitmap image after resizing it.

    Hope it helps you to resize your uploaded image...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: