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

I have my files saved as a file on my hard drive. (Not in the database).

I will get the height and width of these * .jpg files, how to do this?

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

+3
source share
3 answers

Many formats support a header with this information, but others do not ...

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

, , System.Drawing.Image IDisposable, using Dispose() , , . :

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

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

0
source

Source: https://habr.com/ru/post/1787741/


All Articles