CREATING IMAGE OBJECT USING SYSTEM.DRAWING.IMAGE.FROMFILE

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
 1297  View(s)
Ratings:
Rate this:
Hi Expert!

I am trying to get the image dimensions of an image that user selects from list box. Image files are available on FTP server. I am displaying file names in 

a list box for users to select. Upon selection, I want to show the preview of image, for that I want to get dimensions so that I can resize it if i need to.

I am storing file name that is linked to currently selected list item into a string variable. I know that path on the server. I am using following code to 

create the Image object, but having no luck

try
{
     string dir = Session["currentUser"].ToString();
     System.Drawing.Image img = System.Drawing.Image.FromFile("~/Uploads/"+dir+"/"+fName, true);     //ERROR here, it gives me file URL as error message!
}
catch(Exception ex)
{
     lbl_Err.Text = ex.Message;
}
Not sure what is going wrong. Any ideas?

Thank you in advance!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Creating Image object using System.Drawing.Image.FromFile

    Hi Manoj!

    use Server.MapPath to fetch the image from the server.
    As follows

    System.Drawing.Image img = 
          System.Drawing.Image.FromFile(Server.MapPath("Uploads/"+dir+"/"+fName), true);  

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!