[英]How to get the the dimensions of an image file?
I have a file called FPN = "c:\ggs\ggs Access\images\members\1.jpg "
我有一个名为FPN = "c:\ggs\ggs Access\images\members\1.jpg "
的文件
I'm trying to get the dimension of image 1.jpg
, and I'd like to check whether image dimension is valid or not before loading.我正在尝试获取图像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 class System.Windows.Media.Imaging.BitmapDecoder
doesn't read whole file, just metadata. 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;
}
Update 2019-07-07 Dealing with exif
'ed images is a little bit more complicated.更新 2019-07-07处理exif
图像有点复杂。 For some reasons iphones save a rotated image and to compensate they also set "rotate this image before displaying" exif flag as such.由于某些原因,iphone 会保存旋转后的图像并作为补偿,因此它们还设置了“在显示之前旋转此图像”的 exif 标志。
Gif is also a pretty complicated format. Gif 也是一种非常复杂的格式。 It is possible that no frame has full gif size, you have to aggregate it from offsets and frames sizes.可能没有框架具有完整的 gif 大小,您必须从偏移量和框架大小中聚合它。
So I used ImageProcessor instead, which deals with all the problems for me.所以我改用ImageProcessor ,它为我处理了所有问题。 Never checked if it reads the whole file though, because some browsers have no exif
support and I had to save a rotated version anyway.从来没有检查过它是否读取了整个文件,因为有些浏览器没有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
}
Unfortunately, System.Drawing
and ImageProcessor
are supported only on the Windows platform because they are a thin wrapper over GDI (Graphics Device Interface) which is very old Win32 (Windows) unmanaged drawing APIs.不幸的是, System.Drawing
和ImageProcessor
仅在 Windows 平台上受支持,因为它们是 GDI(图形设备接口)的薄包装,这是非常古老的 Win32 (Windows) 非托管绘图 API。
Instead, just install the SixLabors/ImageSharp library from NuGet.相反,只需从 NuGet 安装SixLabors/ImageSharp库。 And here is how you can get size with it:以下是如何使用它来获得大小:
using (var image = SixLabors.ImageSharp.Image.Load("image-path."))
{
var width = image.Width;
var height = image.Height;
}
For .NET Core users and anyone who don't want to use 3rd parity libraries (and like me read the specifications and keep things simple), here is solution for JPEG dimensions:对于 .NET Core 用户和任何不想使用第三奇偶校验库的人(像我一样阅读规范并保持简单),这里是 JPEG 尺寸的解决方案:
public class JPEGPicture
{
private byte[] data;
private ushort m_width;
private ushort m_height;
public byte[] Data { get => data; set => data = value; }
public ushort Width { get => m_width; set => m_width = value; }
public ushort Height { get => m_height; set => m_height = value; }
public void GetJPEGSize()
{
ushort height = 0;
ushort width = 0;
for (int nIndex = 0; nIndex < Data.Length; nIndex++)
{
if (Data[nIndex] == 0xFF)
{
nIndex++;
if (nIndex < Data.Length)
{
/*
0xFF, 0xC0, // SOF0 segement
0x00, 0x11, // length of segment depends on the number of components
0x08, // bits per pixel
0x00, 0x95, // image height
0x00, 0xE3, // image width
0x03, // number of components (should be 1 or 3)
0x01, 0x22, 0x00, // 0x01=Y component, 0x22=sampling factor, quantization table number
0x02, 0x11, 0x01, // 0x02=Cb component, ...
0x03, 0x11, 0x01 // 0x03=Cr component, ...
*/
if (Data[nIndex] == 0xC0)
{
Console.WriteLine("0xC0 information:"); // Start Of Frame (baseline DCT)
nIndex+=4;
if (nIndex < Data.Length-1)
{
// 2 bytes for height
height = BitConverter.ToUInt16(new byte[2] { Data[++nIndex], Data[nIndex-1] }, 0);
Console.WriteLine("height = " + height);
}
nIndex++;
if (nIndex < Data.Length - 1)
{
// 2 bytes for width
width = BitConverter.ToUInt16(new byte[2] { Data[++nIndex], Data[nIndex-1] }, 0);
Console.WriteLine("width = " + width);
}
}
}
}
}
if (height != 0)
Height = height;
if (width != 0)
Width = width;
}
public byte[] ImageToByteArray(string ImageName)
{
FileStream fs = new FileStream(ImageName, FileMode.Open, FileAccess.Read);
byte[] ba = new byte[fs.Length];
fs.Read(ba, 0, Convert.ToInt32(fs.Length));
fs.Close();
return ba;
}
}
class Program
{
static void Main(string[] args)
{
JPEGPicture pic = new JPEGPicture();
pic.Data = pic.imageToByteArray("C:\\Test.jpg");
pic.GetJPEGSize();
}
}
I leave a space for anyone who wants to improve the code for progressive DCT:我为任何想要改进渐进式 DCT 代码的人留出空间:
if (Data[nIndex] == 0xC2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.