Saturday 10 March, 2012

Resizing uploaded images in ASP.Net

ASPX code:
<div style="overflow:hidden;position:relative;width:250px;height:200px;">
    <asp:Image ID="img" runat="server" />
</div>
<asp:FileUpload ID="fileUploadCtrl" runat="server"/>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_OnClick" />

ASPX.CS code:
protected void btnUpload_OnClick(object sender, EventArgs e)
{
    if (fileUploadCtrl.HasFile)
    {
        HttpPostedFile PostedImg = fileUploadCtrl.PostedFile;
        string ftype = PostedImg.ContentType;
        string fname = PostedImg.FileName;
        string ext = fname.ToString().Substring(fname.LastIndexOf("."), fname.Length - fname.LastIndexOf("."));
        if (ext == ".jpg" || ext == ".bmp" || ext == ".gif" || ext == ".png")
        {
            string ImgName = "Image1" + ext;//Your Image name

            #region Original Size
            fileUploadCtrl.SaveAs(Server.MapPath("~/images/original/" + ImgName));
            #endregion

            Bitmap originalBMP = new Bitmap(fileUploadCtrl.PostedFile.InputStream);

            #region Required Size
            string ImgStyle = "";//Required Size Width=250;Height=200

            int resizeheight = 1, resizewidth = 1;
            if (originalBMP.Width > originalBMP.Height)
            {
                double ratio = (double)200 / originalBMP.Height;
                double width = (double)originalBMP.Width * ratio;
                resizewidth = (int)width;
                resizeheight = 200;
            }
            else if (originalBMP.Width <= originalBMP.Height)
            {
                double ratio = (double)250 / originalBMP.Width;
                double height = (double)originalBMP.Height * ratio;
                resizewidth = 250;
                resizeheight = (int)height;
            }

            Bitmap newBMP = new Bitmap(originalBMP, resizewidth, resizeheight);
            Graphics oGraphics = Graphics.FromImage(newBMP);
            oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            oGraphics.DrawImage(originalBMP, 0, 0, resizewidth, resizeheight);
            newBMP.Save(Server.MapPath("~/images/" + ImgName));

            if (originalBMP.Width > originalBMP.Height)
            {
                int margin = FindHeightOrWidth(ImgName, false) - 250;
                int left = margin / 2;
                ImgStyle = "left:" + (-left).ToString() + "px";
            }
            else if (originalBMP.Width <= originalBMP.Height)
            {
                int margin = FindHeightOrWidth(ImgName, true) - 200;
                int top = margin / 2;
                ImgStyle = "top:" + (-top).ToString() + "px";
            }
            originalBMP.Dispose();
            newBMP.Dispose();
            oGraphics.Dispose();
            #endregion


            //Save the ImgName and ImgStyle in database when ever want to show the image the do these things

            img.ImageUrl = "~/images/" + ImgName;
            if (ImgStyle.Contains("top:"))
                img.Style.Add("top", ImgStyle.Replace("top:", ""));
            else
                img.Style.Add("left", ImgStyle.Replace("left:", ""));

            #region To Delete the uploaded images
            File.Delete(Server.MapPath("~/images/original/" + ImgName));
            File.Delete(Server.MapPath("~/images/" + ImgName));
            #endregion
        }
    }
}

public int FindHeightOrWidth(string filename, Boolean ReturnHeightWidth)
{
    FileStream fs = new FileStream(MapPath("~/images/" + filename), FileMode.Open, FileAccess.Read, FileShare.Read);
    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
    int fileWidth = 1, fileHeight = 1;
    try
    {
        fileWidth = image.Width;
        fileHeight = image.Height;
    }
    catch (Exception ex) { }
    return ReturnHeightWidth ? fileHeight : fileWidth;
}


Tag: Implementing image aspect ratio in asp.net, Resizing uploaded images in ASP.Net 

No comments:

Post a Comment

Parsing JSON w/ @ symbol in it

To read the json response like bellow @ concatenated with attribute                             '{ "@id": 1001, "@name&q...