我怎么能知道什么图像格式,我从流中得到什么? [英] How can I know what image format I get from a stream?

查看:87
本文介绍了我怎么能知道什么图像格式,我从流中得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一些Web服务的字节流。这个字节流包含图像的二进制日期和我使用下面的方法将其转换为一个图像的实例。

我需要知道我有什么样的形象。它是一个简单的位图( *。BMP )或JPEG图像( *。JPG )或PNG图像?

我如何才能找到它呢?

 公共静态图像byteArrayToImage(字节[] bmpBytes)
    {
        形象画像= NULL;
        使用(MemoryStream的流=新的MemoryStream(bmpBytes))
        {
            图像= Image.FromStream(流);
        }        返回形象;
    }


解决方案

您可以检出<一个href=\"http://msdn.microsoft.com/en-us/library/system.drawing.image.rawformat.aspx\"><$c$c>Image.RawFormat属性。所以一旦你加载从流的形象,你可以测试:

 如果(ImageFormat.Jpeg.Equals(image.RawFormat))
{
    // JPEG
}
否则,如果(ImageFormat.Png.Equals(image.RawFormat))
{
    // PNG
}
否则,如果(ImageFormat.Gif.Equals(image.RawFormat))
{
    // GIF
}
...等

I get a byte stream from some web service. This byte stream contains the binary date of an image and I'm using the method below to convert it to an Image instance.

I need to know what kind of image I've got. Is it a simple bitmap (*.bmp) or a JPEG image (*.jpg) or a png image?

How can I find it out?

    public static Image byteArrayToImage( byte[] bmpBytes )
    {
        Image image = null;
        using( MemoryStream stream = new MemoryStream( bmpBytes ) )
        {
            image = Image.FromStream( stream );
        }

        return image;
    }

解决方案

You may checkout the Image.RawFormat property. So once you load the image from the stream you could test:

if (ImageFormat.Jpeg.Equals(image.RawFormat))
{
    // JPEG
}
else if (ImageFormat.Png.Equals(image.RawFormat))
{
    // PNG
}
else if (ImageFormat.Gif.Equals(image.RawFormat))
{
    // GIF
}
... etc

这篇关于我怎么能知道什么图像格式,我从流中得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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