如何使用Stream获取图像大小(wxh)

Mat*_*ics 14 c# asp.net image file stream

我有这个代码我用来读取上传的文件,但我需要获得图像的大小,但不知道我可以使用什么代码

HttpFileCollection collection = _context.Request.Files;
            for (int i = 0; i < collection.Count; i++)
            {
                HttpPostedFile postedFile = collection[i];

                Stream fileStream = postedFile.InputStream;
                fileStream.Position = 0;
                byte[] fileContents = new byte[postedFile.ContentLength];
                fileStream.Read(fileContents, 0, postedFile.ContentLength);
Run Code Online (Sandbox Code Playgroud)

我可以正确地获取文件但是如何检查它的图像(宽度和大小)先生?

Rob*_*Rob 37

首先你必须写下图像:

System.Drawing.Image image = System.Drawing.Image.FromStream (new System.IO.MemoryStream(byteArrayHere));
Run Code Online (Sandbox Code Playgroud)

然后你有:

image.Height.ToString(); 
Run Code Online (Sandbox Code Playgroud)

image.Width.ToString();
Run Code Online (Sandbox Code Playgroud)

注意:您可能需要添加一项检查,以确保它是上传的图像?

  • 嘿,把它放在"使用"条款中! (14认同)