Home > ASP.Net > Resize Image Before Upload

Resize Image Before Upload


Hi

try this example to allow user to upload image with fixed extension and higth and width

but if user image grater than this higth and width will resize his image

public bool CheckFileType(string fileName, string MimeType)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                if (MimeType.ToLower() == "image/gif")
                {
                    return true;
                }
                else
                {
                    return false;
                }

                break;
            case ".png":
                if (MimeType.ToLower() == "image/png")
                {
                    return true;
                }
                else if (MimeType.ToLower() == "image/x-png")
                {
                    return true;
                }
                else
                {
                    return false;
                }

                break;
            case ".jpg":
                if (MimeType.ToLower() == "image/jpeg")
                {
                    return true;
                }
                else if (MimeType.ToLower() == "image/pjpeg")
                {
                    return true;
                }
                else
                {
                    return false;
                }

                break;
            case ".jpeg":
                if (MimeType.ToLower() == "image/jpeg")
                {
                    return true;
                }
                else if (MimeType.ToLower() == "image/pjpeg")
                {
                    return true;
                }
                else
                {
                    return false;
                }

                break;
            case ".bmp":
                if (MimeType.ToLower() == "image/bmp")
                {
                    return true;
                }
                else
                {
                    return false;
                }

                break;
            default:
                return false;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        const int bmpW = 464;// //New image target width
        const int bmpH = 620;// //New Image target height

        if (FileUpload1.HasFile)
        {
            //Clear the error label text
            lblError.Text = "";

            //Check to make sure the file to upload has a picture file format extention
            //and set the target width and height

            if (this.CheckFileType(FileUpload1.FileName, FileUpload1.PostedFile.ContentType))
            {
                Int32 newWidth = bmpW;

                Int32 newHeight = bmpH;

                //Use the uploaded filename for saving without the "." extension

                String upName = FileUpload1.FileName.Substring(0, FileUpload1.FileName.IndexOf("."));
                //Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, ".") - 1)) ;

                //Set the save path of the resized image, you will need this directory already created in your web site

                String filePath = "~/Upload/" + upName + ".jpg";

                //Create a new Bitmap using the uploaded picture as a Stream
                //Set the new bitmap resolution to 72 pixels per inch

                //This is just for definition
                Bitmap upBmp = (Bitmap)Bitmap.FromStream(FileUpload1.PostedFile.InputStream);

                Int32 upWidth = upBmp.Width;

                Int32 upHeight = upBmp.Height;
               
                Bitmap newBmp = new Bitmap(620, 464, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                // You can change the 620 or 464 on any width and heght you want, and the aspect ratio remains good if respect the patterns
                if (upWidth > upHeight)
                {
                    if (upWidth <= 620 && upHeight <= 464)
                    {
                        newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = upWidth;
                        newHeight = upHeight;
                    }
                    else
                    {
                        newBmp = new Bitmap(620, 464, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = 620;
                        newHeight = 464;
                    }
                }
                else if (upWidth < upHeight)
                {
                    if (upWidth <= 464 && upHeight <= 620)
                    {
                        newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = upWidth;
                        newHeight = upHeight;
                    }
                    else
                    {
                        newBmp = new Bitmap(464, 620, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = 464;
                        newHeight = 620;
                    }
                }
                else if (upWidth == upHeight)
                {
                    if (upWidth <= 500 && upHeight <= 500)
                    {
                        newBmp = new Bitmap(upWidth, upHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = upWidth;
                        newHeight = upHeight;
                    }
                    else
                    {
                        newBmp = new Bitmap(500, 500, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        newWidth = 500;
                        newHeight = 500;
                    }
                }
                newBmp.SetResolution(72, 72);

                //Get the uploaded image width and height

                Int32 newX = 0; //Set the new top left drawing position on the image canvas

                Int32 newY = 0;

                //Create a new image from the uploaded picture using the Graphics class

                //Clear the graphic and set the background colour to white

                //Use Antialias and High Quality Bicubic to maintain a good quality picture

                //Save the new bitmap image using //Png// picture format and the calculated canvas positioning

                Graphics newGraphic = Graphics.FromImage(newBmp);

                try
                {
                    newGraphic.Clear(Color.Empty);

                    newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                    newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                    newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight);

                    newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);

                    //Show the uploaded resized picture in the image control

                    Image1.ImageUrl = filePath;

                    Image1.Visible = true;

                }
                catch (Exception ex)
                {
                    lblError.Text = ex.ToString();
                    throw ex;
                }
                finally
                {
                    upBmp.Dispose();

                    newBmp.Dispose();

                    newGraphic.Dispose();
                }
            }
            else
            {
                lblError.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png.";

            }
        }
    }

Good Luck

Advertisements
Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: