二进制转换使用内存流位图 [英] convert binary to bitmap using memory stream

查看:106
本文介绍了二进制转换使用内存流位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想转换成二进制阿雷位图和图像显示在PictureBox我写下面的code,但我得到的例外,它说parametr是无效的。

 公共静态位图ByteToImage(byte []的BLOB)
    {
        MemoryStream的Mstream工具=新的MemoryStream();
        字节[]的pData =斑点;
        mStream.Write(pData所,0,Convert.ToInt32(pData.Length));
        位图BM =新位图(Mstream工具);
        mStream.Dispose();
        返回BM;

    }
 

解决方案

这要看是什么 BLOB 。它是一个有效的位图格式(如PNG,BMP,GIF等?)。如果它是关于位图中的像素原始字节的信息,你不能这样做。

这可能有助于在使用回卷流开始 mStream.Seek(0,SeekOrigin.Begin)前行位图BM =新位图(Mstream工具);

 公共静态位图ByteToImage(byte []的BLOB)
{
    使用(MemoryStream的Mstream工具=新的MemoryStream())
    {
         mStream.Write(一滴,0,blob.Length);
         mStream.Seek(0,SeekOrigin.Begin);

         位图BM =新位图(Mstream工具);
         返回BM;
    }
}
 

hi i wanna convert binary arrey to bitmap and show image in a picturebox i write following code but i get exception that it say parametr is not valid .

  public static Bitmap ByteToImage(byte[] blob)
    {
        MemoryStream mStream = new MemoryStream();
        byte[] pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
        Bitmap bm = new Bitmap(mStream);
        mStream.Dispose();
        return bm;

    }

解决方案

It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.

It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.

public static Bitmap ByteToImage(byte[] blob)
{
    using (MemoryStream mStream = new MemoryStream())
    {
         mStream.Write(blob, 0, blob.Length);
         mStream.Seek(0, SeekOrigin.Begin);

         Bitmap bm = new Bitmap(mStream);
         return bm;
    }
}

这篇关于二进制转换使用内存流位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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