This is a migrated thread and some comments may be shown as answers.

RADUpload auto resize width only

1 Answer 174 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
sammy ymmas
Top achievements
Rank 1
sammy ymmas asked on 08 Jun 2010, 10:45 AM
Hi!
I would like to resize files that are being uploaded automatically. But I only want to resize the width. Regarding the height of the file, this should auto adjust it self. Forexample; If I'm uploading a file that is 1000 px in width and 2000px in height and then resize it to 500px in width, then I want the height to be auto resized to 1000px. In other words the height and width has to be proportional.

This is the code that I'm using right now.

  int width = 500;
  int height = 500;
  using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, height, thumbnailImageAbortDelegate, IntPtr.Zero))
  {
      string thumbnailFileName = Path.Combine(target, string.Format("{0}_BigPicture{1}", file.GetNameWithoutExtension(), file.GetExtension()));
      thumbnail.Save(thumbnailFileName);

The problem with this is that is has to take both the height and the width. I cannot leave out the height parameter.

P.S. My DLL version is 2009.3.1208.35 > Telerik.Web.UI.dll (ASP.NET Ajax)

So I hope someone can help.

Thanks

1 Answer, 1 is accepted

Sort by
0
sammy ymmas
Top achievements
Rank 1
answered on 15 Jun 2010, 08:46 AM
Hi guys!
I finally found a solution through help from forums.asp.net

The code below is made under the assumption that the width of the image will always be resized to 500px and the height will be adjusted proportionally.

protected void Button1_Click(object sender, EventArgs e)   
{   
    string target = Server.MapPath("~/Images");   
    System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);   
   
    foreach (UploadedFile file in RadUpload1.UploadedFiles)   
    {   
        using (Bitmap originalImage = new Bitmap(file.InputStream))   
        {   
            decimal w1, h1, width2;   
            w1 = originalImage.Width;   
            h1 = originalImage.Height;   
            width2 = calculations(w1, h1);   
   
            int height = Convert.ToInt32(width2);   
            int width = 500;   
            using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, height, thumbnailImageAbortDelegate, IntPtr.Zero))   
            {   
                string thumbnailFileName =  Path.Combine(target, string.Format("{0}_thumb{1}", file.GetNameWithoutExtension(), file.GetExtension()));   
                thumbnail.Save(thumbnailFileName);   
            }   
        }   
    }   
}   
   
public bool ThumbnailCallback()   
{   
    return false;   
}   
   
   
public decimal calculations(decimal w1, decimal h1)   
{    
    decimal height = 0;   
    decimal ratio = 0;   
    const int myWidth = 500;   
       
      
    if (myWidth < w1)   
    {   
        ratio = w1 / myWidth;   
        height = h1 / ratio;   
   
        return height;   
    }   
   
     if (w1 < myWidth)   
    {   
        ratio = myWidth / w1;   
        height = h1 * ratio;   
        return height;   
    }   
   
    return height;   
}   

Well that's it. Now the height will always resize itself proportionally. 

Tags
Upload (Obsolete)
Asked by
sammy ymmas
Top achievements
Rank 1
Answers by
sammy ymmas
Top achievements
Rank 1
Share this question
or