首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在不加载图像的情况下可靠地获取.NET中的图像尺寸?

在不加载图像的情况下可靠地获取.NET中的图像尺寸,可以使用以下方法:

  1. 使用System.Drawing命名空间中的Image类的FromFile方法打开图像文件,然后读取其宽度和高度属性。
代码语言:csharp
复制
using System.Drawing;

string imagePath = "path/to/image.jpg";
Image image = Image.FromFile(imagePath);
int width = image.Width;
int height = image.Height;
  1. 使用System.IO.File类的ReadAllBytes方法读取图像文件的二进制数据,然后使用MemoryStream类将其转换为Image对象,再读取其宽度和高度属性。
代码语言:csharp
复制
using System.IO;
using System.Drawing;

string imagePath = "path/to/image.jpg";
byte[] imageData = File.ReadAllBytes(imagePath);
using MemoryStream ms = new MemoryStream(imageData);
Image image = Image.FromStream(ms);
int width = image.Width;
int height = image.Height;
  1. 使用System.IO.File类的ReadAllBytes方法读取图像文件的二进制数据,然后使用System.Drawing.Bitmap类的FromStream方法将其转换为Image对象,再读取其宽度和高度属性。
代码语言:csharp
复制
using System.IO;
using System.Drawing;

string imagePath = "path/to/image.jpg";
byte[] imageData = File.ReadAllBytes(imagePath);
using MemoryStream ms = new MemoryStream(imageData);
Bitmap bitmap = new Bitmap(ms);
int width = bitmap.Width;
int height = bitmap.Height;

以上方法均可在不加载图像的情况下可靠地获取.NET中的图像尺寸。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券