user1889838
user1889838

Reputation: 343

Validate uploaded file as image and then get the dimensions of it

I want to upload an image file to FTP server, so I have an AsyncFileUpload control on asp.net page. I am using following code to get the type of AsyncFileUpload1.PostedFile and then trying to get the dimensions of it but having no luck so far.

string cType =asyncFU.PostedFile.ContentType;
if (cType.Contains("image"))
{
   Stream ipStream = asyncFU.PostedFile.InputStream;
   Image img = System.Drawing.Image.FromStream(ipStream); //ERROR comes here, I can't think the work around this.
   int w = img.PhysicalDimension.Width;
   int h = img.PhysicalDimension.Height;
}

As you can see, errors message says that it cannot convert from System.Drawing.Image to System.Web.UI.WebControls.Image. i understand the error, but I cannot think the work around this.

I have an AsyncFileUpload control where unknown file will be uploaded, but ONLY saved to FTP server if it is an image file.

Any suggestions?

Upvotes: 1

Views: 1419

Answers (2)

Khaled Tarboosh
Khaled Tarboosh

Reputation: 31

try this :

Stream ipStream = fuAttachment.PostedFile.InputStream;
        using (var image = System.Drawing.Image.FromStream(ipStream))
        {                    
            float w = image.PhysicalDimension.Width;
            float h = image.PhysicalDimension.Height;
        }

Upvotes: 1

John Willemse
John Willemse

Reputation: 6698

Your img variable should be of the type System.Drawing.Image, not System.Web.UI.WebControls.Image.

Upvotes: 0

Related Questions