Description: While working on asp.net application it is sometimes required to resize the image as per application requirement. Suppose there is image upload functionality in your website where user can upload their pictures. In this case you must resize the image before storing so that web space requirement for storing can be reduced.

Implementation: Let's understand by creating a demo website.

Source Code:

  • In the design page (.aspx) place a FileUpload Control and a Button control as:
<table>
        <tr>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                      <asp:Button ID="btnUpload" runat="server" Text="Submit"
            onclick="btnUpload_Click" /></td>
         </tr>       
  </table>
C#.NET Code to  upload and resize the images
  • In the code behind file (.aspx.cs) write the code as:
First include following namespaces:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.IO;
  • Then write the code as:
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string img=string.Empty;
            Bitmap bmpImg=null;
            try
            {
                bmpImg = Resize_Image(FileUpload1.PostedFile.InputStream, 210, 130);
                img = Server.MapPath("images/") + Guid.NewGuid().ToString() + “.png";
                bmpImg.Save(img, ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
            finally
            {
                img = string.Empty;
                bmpImg.Dispose();
            }      
          }
    }
  
private Bitmap Resize_Image(Stream streamImage, int maxWidth, int maxHeight)
    {
        Bitmap originalImage = new Bitmap(streamImage);
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = Convert.ToDouble(originalImage.Width) / Convert.ToDouble(originalImage.Height);
        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = Convert.ToInt32(Math.Round(newWidth / aspectRatio));
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = Convert.ToInt32(Math.Round(newHeight * aspectRatio));
        }
        return new Bitmap(originalImage, newWidth, newHeight);
    }

Note: Uploaded image will be stored in the “images” folder of the root directory in our case. You can change the folder name and location as per your requirement.  Now you can display the resized image on your website.

1 comments:

  1. I was looking at some of your posts on this website and I conceive this web site is really instructive! Keep putting up..
    itcs代写

    ReplyDelete

 
Top