如何从文件名获取图片尺寸

我有一个文件叫FPN =“c:\ ggs \ ggs Access \ images \ members \ 1.jpg”

我试图获得图像1.jpg的维度,并且我想在加载前检查图像尺寸是否有效,如果图像的宽度或高度小于或等于零,popup像“图像格式不正确”的消息

任何人都可以帮我吗?

System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\ggs\ggs Access\images\members\1.jpg"); MessageBox.Show("Width: " + img.Width + ", Height: " + img.Height); 

Wpf类System.Windows.Media.Imaging.BitmapDecoder不读取整个文件,只是元数据。

 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; }