我有一个名为FPN = "c:\ggs\ggs Access\images\members\1.jpg "
的文件
我正在尝试获取图像1.jpg
的尺寸,我想在加载之前检查图像尺寸是否有效。
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;
}
更新2019-07-07处理exif
图像比较复杂。由于某些原因,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
}