how can I get image size (w x h) using Stream

后端 未结 3 1929
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 04:11

I have this code i am using to read uploaded file, but i need to get size of image instead but not sure what code can i use

HttpFileCollection collection = _cont         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 04:32

    Read the image into a buffer (you either have a Stream to read from or the byte[], because if you had the Image you'd have the dimensions anyway).

    public Size GetSize(byte[] bytes)
    {
       using (var stream = new MemoryStream(bytes))
       {
          var image = System.Drawing.Image.FromStream(stream);
    
          return image.Size;
       }
    }
    

    You can then go ahead and get the image dimensions:

    var size = GetSize(bytes);
    
    var width = size.Width;
    var height = size.Height;
    

提交回复
热议问题