一、您可以使用 System.Drawing.Image 类

System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("example.gif"));
int width = img.Width;
int height = img.Height;
img.Dispose();

Image 的名称空间是:System.Drawing

但是这里我们却不能在导入名称空间后使用 Image 时省略 System.Drawing,因为这会和 System.Web.UI.WebControls 的 Image 产生混淆。可以处理 BMP、JPEG、GIF、PNG 等格式的图片。

二、或者您也可以使用 System.Drawing.Bitmap 类

Bitmap bmp = new Bitmap(Server.MapPath("example.jpg"));
int width = bmp.Width;
int height = bmp.Height;
bmp.Dispose();

Bitmap 的名称空间是:System.Drawing

虽然名称是 Bitmap,但实际同样可以处理 BMP、JPEG、GIF、PNG 等格式的图片。

三、那么 System.Drawing.Image 和 System.Drawing.Bitmap 有什么区别呢?

System.Drawing.Image 其实是 System.Drawing.Bitmap 的基类,这就是为什么 Bitmap 不单单可以处理 BMP 图片的原因了。

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2021-11-13
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-05-04
  • 2021-09-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案