How to get image size from file name

I have a file named FPN = "c: \ ggs \ ggs Access \ images \ members \ 1.jpg"

I am trying to get the image size 1.jpg, and I would like to check whether the image size is valid or not before loading, and if the width or height of the image is less than or equal to zero, a message like "image is not in the correct format" will appear

Can anybody help me?

+52
c # image dimensions
Jun 23 2018-11-11T00:
source share
2 answers
System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\ggs\ggs Access\images\members\1.jpg"); MessageBox.Show("Width: " + img.Width + ", Height: " + img.Height); 
+112
Jun 23 2018-11-11T00:
source share

The wpf class System.Windows.Media.Imaging.BitmapDecoder does not read the entire file, only metadata.

 using(var imageStream = File.OpenRead("file")) { var decoder = BitmapDecoder.Create(imageStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default); var height = decoder.Frames[0].PixelHeight; var width = decoder.Frames[0].PixelWidth; } 

Update 2019-07-07

If I remember correctly, after a few months I found that working with exif images is a little more difficult. For some reason, the iphone saves the rotated image instead of the usual one and sets the flag β€œrotate this image before displaying”.
GIF also turned out to be a rather complicated format. It is possible that not a single frame has a full GIF size, you should aggregate it from offsets and frame sizes.

So I used ImageProcessor instead, which handled all the problems for me. I never checked if it reads the whole file, although some browsers do not have exif support, and I still had to save the rotated version.

 using (var imageFactory = new ImageFactory()) { imageFactory .Load(stream) .AutoRotate(); //takes care of ex-if var height = imageFactory.Image.Height, var width = imageFactory.Image.Width } 
+32
Jun 27 '16 at 4:15
source share



All Articles