【发布时间】:2011-09-21 07:25:01
【问题描述】:
我有一个名为FPN = "c:\ggs\ggs Access\images\members\1.jpg "的文件
我正在尝试获取图像1.jpg的尺寸,我想在加载前检查图像尺寸是否有效。
【问题讨论】:
-
我所知道的没有图像格式可以具有 0 像素的尺寸...
标签: c# image dimensions
我有一个名为FPN = "c:\ggs\ggs Access\images\members\1.jpg "的文件
我正在尝试获取图像1.jpg的尺寸,我想在加载前检查图像尺寸是否有效。
【问题讨论】:
标签: c# image dimensions
System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\ggs\ggs Access\images\members\1.jpg");
MessageBox.Show("Width: " + img.Width + ", Height: " + img.Height);
【讨论】:
Image.FromStream,这样您就可以避免将整个文件加载到内存中(并且更快)——参见roelvanlisdonk.nl/2012/02/28/…
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;
}
2019-07-07 更新
处理exif'ed 图像有点复杂。由于某些原因,iphone 会保存旋转的图像并作为补偿,他们还设置了“在显示前旋转此图像”exif 标志。
Gif 也是一种相当复杂的格式。可能没有帧具有完整的 gif 大小,您必须从偏移量和帧大小聚合它。
所以我改用ImageProcessor,它为我解决了所有问题。从来没有检查过它是否读取了整个文件,因为有些浏览器不支持exif,我不得不保存一个旋转的版本。
using (var imageFactory = new ImageFactory())
{
imageFactory
.Load(stream)
.AutoRotate(); //takes care of ex-if
var height = imageFactory.Image.Height,
var width = imageFactory.Image.Width
}
【讨论】:
ImageProcessor给我imageprocessor.org/imageprocessor/imagefactory/#example和Getting Started有Install Via Nuget部分。