简体   繁体   English

验证上传的文件为图像,然后获取其尺寸

[英]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. 我想将图像文件上传到FTP服务器,所以我在asp.net页面上有一个AsyncFileUpload控件。 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. 我正在使用以下代码来获取AsyncFileUpload1.PostedFile的类型,然后尝试获取其尺寸,但到目前为止还没有运气。

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 . 如您所见,错误消息表明它无法从System.Drawing.Image转换为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. 我有一个AsyncFileUpload控件,其中将上传未知文件,但是如果它是图像文件,则仅保存到FTP服务器。

Any suggestions? 有什么建议么?

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;
        }

您的img变量应为System.Drawing.Image类型,而不是System.Web.UI.WebControls.Image类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM