我怎么能知道什么图像格式,我从流中得到什么? [英] How can I know what image format I get from a stream?
问题描述
我从一些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$c$c>属性。所以一旦你加载从流的形象,你可以测试:
如果(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屋!