使用位图对象在C#中查找图像格式 [英] Find image format using Bitmap object in C#

查看:242
本文介绍了使用位图对象在C#中查找图像格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载图像文件的硬盘驱动器的二进制字节,并加载到一个位图对象。如何找到图像类型[JPEG,PNG,BMP等]从位图对象?

看起来微不足道的。但是,不能看着办吧!

是否有其他方法吗?

鸭preciate你的回应。

修订正确的解决方法:

@CMS:感谢您的正确的响应

样品code来实现这一点。

 使用(MemoryStream的imageMemStream =新的MemoryStream(的FileData))
{
    使用(位图位图=新位图(imageMemStream))
    {
        的imageformat的imageformat = bitmap.RawFormat;
        如果(bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            //这是一个JPEG;
        否则,如果(bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            //这是一个PNG;
    }
}
 

解决方案

如果你想知道的图像格式,可以加载使用的图片类,并检查它的 RawFormat 属性:

 使用(图片IMG = Image.FromFile(@C:\路径\到\ img.jpg))
{
    如果(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
    {
      // ...
    }
}
 

I am loading the binary bytes of the image file hard drive and loading it into a Bitmap object. How do i find the image type[JPEG, PNG, BMP etc] from the Bitmap object?

Looks trivial. But, couldn't figure it out!

Is there an alternative approach?

Appreciate your response.

UPDATED CORRECT SOLUTION:

@CMS: Thanks for the correct response!

Sample code to achieve this.

using (MemoryStream imageMemStream = new MemoryStream(fileData))
{
    using (Bitmap bitmap = new Bitmap(imageMemStream))
    {
        ImageFormat imageFormat = bitmap.RawFormat;
        if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            //It's a JPEG;
        else if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            //It's a PNG;
    }
}

解决方案

If you want to know the format of an image, you can load the file with the Image class, and check its RawFormat property:

using(Image img = Image.FromFile(@"C:\path\to\img.jpg"))
{
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
    {
      // ...
    }
}

这篇关于使用位图对象在C#中查找图像格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆