locked
help on resizing image RRS feed

  • Question

  • User-412682272 posted

    Hello, Im trying to resize photos right after they are uploaded through an FileUpload control with ASP.NET/C#.


    protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filePath = Server.MapPath(".\\library\\");
            string strFileName = FileUpload1.PostedFile.FileName;
    
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(filePath + "UID" + lblLoginID.Text + "_" + strFileName);
    
                DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(".\\library\\"));
                lvwLibraryItems.DataSource = dirInfo.GetFiles("*");
                lvwLibraryItems.DataBind();
                lblMessage.Text = "File Uploaded: " + strFileName;
    
                //create a thumbnail
                string filePathOriginal = Server.MapPath(".\\library\\") + "UID" +       lblLoginID.Text + "_" + strFileName;
                string filePath50x50 = Server.MapPath(".\\library\\50x50\\") + "UID" + lblLoginID.Text + "_" + strFileName;
                string filePath400x400 = Server.MapPath(".\\library\\400x400\\") + "UID" + lblLoginID.Text + "_" + strFileName;
                
                System.Drawing.Size NewSize50x50 = new System.Drawing.Size(50, 50);
                System.Drawing.Size NewSize400x400 = new System.Drawing.Size(400, 400);
                
                ResizePicture(filePathOriginal, filePath400x400, NewSize400x400);
                ResizePictureSquare(filePathOriginal, filePath50x50, NewSize50x50);
    
            }
            else
            {
                lblMessage.Text = "No File Uploaded.";
            }     
        }
    
    public void ResizePictureSquare(string originalpath, string newpath, Size newsize)
        {
            using (Bitmap newbmp = new Bitmap(newsize.Width, newsize.Height), oldbmp = Bitmap.FromFile(originalpath) as Bitmap)
            {
                using (Graphics newgraphics = Graphics.FromImage(newbmp))
                {
                    newgraphics.Clear(Color.FromArgb(-1));
                    //this line added to set interpolationMode to high quality
                    newgraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                    if ((float)oldbmp.Width / (float)newsize.Width == (float)oldbmp.Height / (float)newsize.Height) //Target size has a 1:1 aspect ratio
                    {
                        newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, newsize.Height);
                    }
                    else if ((float)oldbmp.Width / (float)newsize.Width > (float)oldbmp.Height / (float)newsize.Height) //There will be white space on the top and bottom
                    {
                        newgraphics.DrawImage(oldbmp, 0f, (float)newbmp.Height / 2f - (oldbmp.Height * ((float)newbmp.Width / (float)oldbmp.Width)) / 2f, (float)newbmp.Width, oldbmp.Height * ((float)newbmp.Width / (float)oldbmp.Width));
                    }
                    else if ((float)oldbmp.Width / (float)newsize.Width < (float)oldbmp.Height / (float)newsize.Height) //There will be white space on the sides
                    {
                        newgraphics.DrawImage(oldbmp, (float)newbmp.Width / 2f - (oldbmp.Width * ((float)newbmp.Height / (float)oldbmp.Height)) / 2f, 0f, oldbmp.Width * ((float)newbmp.Height / (float)oldbmp.Height), (float)newbmp.Height);
                    }
                    newgraphics.Save();
                    newgraphics.Dispose();
                    newbmp.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    newbmp.Dispose();
                    oldbmp.Dispose();
                    
                }
            }
        }
    
        public static void ResizePicture(string originalpath, string newpath, Size newsize)
        {
            using (Bitmap oldbmp = Bitmap.FromFile(originalpath) as Bitmap)
            {
                if (oldbmp.Width > oldbmp.Height)
                {
                    //landscape
                    newsize.Height = oldbmp.Height / (oldbmp.Width / newsize.Width);
                }
                else if (oldbmp.Height > oldbmp.Width)
                {
                    //portrait
                    newsize.Width = oldbmp.Width / (oldbmp.Height / newsize.Height);
                }
                else if (oldbmp.Width == oldbmp.Height)
                {
                    //square
                    if (newsize.Width > newsize.Height)//get the shortest side
                        newsize.Width = newsize.Height;
                    else
                        newsize.Height = newsize.Width;
                }
    
                using (Bitmap newbmp = new Bitmap(newsize.Width, newsize.Height))
                {
                    using (Graphics newgraphics = Graphics.FromImage(newbmp))
                    {
                        newgraphics.Clear(Color.FromArgb(-1));
                        //this line added to set interpolationMode to high quality
                        newgraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, newsize.Height);
                        newgraphics.Save();
                        newgraphics.Dispose();
                        newbmp.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        newbmp.Dispose();
                        oldbmp.Dispose();
                    }
                }
            } //end using oldbmp
        }//end method


    As you can see, I'm keeping an original copy in "library" folder, then creating a square 50x50 pixel copy at "library\50x50" folder, then supposedly an original aspect ratio with a max width/height (whichever is longer) of 400px saved to "library\400x400" folder

    My problem is the 400x400 code seems to not work. It only resizes one side to 400px and keeps the other side's original height/width so it distorts the image. Is something wrong with this code?

    I got these snippets from an early thread here that came out with search. The 50x50 resize works (with white space). 

    Thanks!

    Wednesday, December 9, 2009 10:56 PM

Answers

  • User-412682272 posted

    Ok, as I suspected, it's got something to do with the DataTypes, by default they are Int, so if there's a decimal when it divides, theres a problem. I casted the division numbers to float first then converted them to Int after division, now it works!


    public void ResizePicture(string originalpath, string newpath, Size newsize)
        {
            using (Bitmap oldbmp = Bitmap.FromFile(originalpath) as Bitmap)
            {
                if (oldbmp.Width > oldbmp.Height)
                {
                    //landscape
                    float ResizedHeight = (float) ((float)oldbmp.Height / ((float)oldbmp.Width / (float)600));
                    newsize.Height = Convert.ToInt32(ResizedHeight);
                }
                else if (oldbmp.Height > oldbmp.Width)
                {
                    //portrait
                    float ResizedWidth = (float)((float)oldbmp.Width / ((float)oldbmp.Height / (float)600));
                    newsize.Width = Convert.ToInt32(ResizedWidth);
                }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 14, 2009 8:54 PM

All replies

  • User2115139740 posted

    just check below link :-


    How to make image thumbnail using ASP.NET(C#)
    http://www.c-sharpcorner.com/UploadFile/raj1979/ImageThumbnail11062008011338AM/ImageThumbnail.aspx


    Wednesday, December 9, 2009 11:04 PM
  • User-412682272 posted

    manoj thanks for the suggestion, but i really want to make the original code to work.


    could it be that Size is integer so it rounds off numbers when it divides?

    Saturday, December 12, 2009 2:21 AM
  • User2115139740 posted

    check this code, what I want is height and width should be less then 100. You can make it below code to work according to ur requirement.


    1. {
    2.     if ((bitMain.Width < bitMain.Height)) {
    3.         if (bitMain.Height > 100) {
    4.             myThumbnailMain = bitMain.GetThumbnailImage(100 * bitMain.Width / bitMain.Height, 100, myCallback, IntPtr.Zero);
    5.         }
    6.         else {
    7.             myThumbnailMain = bitMain.GetThumbnailImage(bitMain.Width, bitMain.Height, myCallback, IntPtr.Zero);
    8.            
    9.         }
    10.     }
    11.     else {
    12.         if (bitMain.Width > 100) {
    13.             myThumbnailMain = bitMain.GetThumbnailImage(100, bitMain.Height * 100 / bitMain.Width, myCallback, IntPtr.Zero);
    14.         }
    15.         else {
    16.             myThumbnailMain = bitMain.GetThumbnailImage(bitMain.Width, bitMain.Height, myCallback, IntPtr.Zero);
    17.         }
    18.     }
    19. }
    Sunday, December 13, 2009 10:58 PM
  • User-412682272 posted

    Hi Manoj, thanks for the reply, but im trying to not use .GetThumbnailimage because I'm resizing to maxwidth of 600px and 400px. .GetThumnnailimage is giving me not so good image quality.


    So far, the math in this code seems to be correct, but when debugging, "newsize.Width/newsize.Height" returns 0 and reverts to originalimage. I suspect something's wrong with the syntax? I tried dividing floats then casting them to int, but it still wont work:

            using (Bitmap oldbmp = Bitmap.FromFile(originalpath) as Bitmap)  
            {  
                if (oldbmp.Width > oldbmp.Height)  
                {  
                    //landscape  
                    newsize.Height = oldbmp.Height / (oldbmp.Width / newsize.Width);  
                }  
                else if (oldbmp.Height > oldbmp.Width)  
                {  
                    //portrait  
                    newsize.Width = oldbmp.Width / (oldbmp.Height / newsize.Height);  
                }  
            .....





    Monday, December 14, 2009 9:17 AM
  • User-412682272 posted

    Ok, as I suspected, it's got something to do with the DataTypes, by default they are Int, so if there's a decimal when it divides, theres a problem. I casted the division numbers to float first then converted them to Int after division, now it works!


    public void ResizePicture(string originalpath, string newpath, Size newsize)
        {
            using (Bitmap oldbmp = Bitmap.FromFile(originalpath) as Bitmap)
            {
                if (oldbmp.Width > oldbmp.Height)
                {
                    //landscape
                    float ResizedHeight = (float) ((float)oldbmp.Height / ((float)oldbmp.Width / (float)600));
                    newsize.Height = Convert.ToInt32(ResizedHeight);
                }
                else if (oldbmp.Height > oldbmp.Width)
                {
                    //portrait
                    float ResizedWidth = (float)((float)oldbmp.Width / ((float)oldbmp.Height / (float)600));
                    newsize.Width = Convert.ToInt32(ResizedWidth);
                }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 14, 2009 8:54 PM