开发者

How to get ratio of width x height of an image on disk / file?

开发者 https://www.devze.com 2023-02-06 12:00 出处:网络
I have my userpictures saved as file on my HDD. (Not in database). I will get the height and width of th开发者_如何学Gois *.jpg-files, how to do that?

I have my userpictures saved as file on my HDD. (Not in database).

I will get the height and width of th开发者_如何学Gois *.jpg-files, how to do that?

(Background: I must calculate the ratio between hight and width to bring it to a specified height and width without stretching it).


I think it is also worth mentioning that System.Drawing.Image implements IDisposable, so you should always use a using statement or call the Dispose() method on your object to make sure that you are releasing any unmanaged system resources. Example:

using (Image img = System.Drawing.Image.FromFile(path))
{
    int height = img.Height;
    int width = img.Width;
}


Many formats support a header with that info but others don't so...

 Image img = System.Drawing.Image.FromFile(path);
 int height = img.Height;
 int width = img.Width;


Use the FromFile static method on the System.Drawing.Bitmap class to load your images. The returned Bitmap instance has Width and Height properties. Altnatively, you can also construct a new Bitmap passing a string path to your image file as a constructor parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消