ASP.NET判断uploadfile上传图片文件

来源:互联网 发布:如何阅读java源代码 编辑:程序博客网 时间:2024/05/18 06:36

自己需要判断文件的像素宽高,网上找了些都是判断图片文件大小和类型的,参考网上的资料,实现代码如下

 HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象 
            Stream sr = hp.InputStream;
            System.Drawing.Image image = System.Drawing.Image.FromStream(sr);
            int width = image.Width;
            int height = image.Height;
            string ext = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();


            image.Dispose();


            if (width > 40 || height > 40 || ext != ".png")
            {
                string strErrMsg = "alert('图片允许上传小于 40像素x40像素 的PNG文件.\\r\\n')";
                Response.Write(JS.RegJs(strErrMsg));
                //FileUpload1.Dispose();
            }

0 0