简体   繁体   中英

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

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

I will get the height and width of this *.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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