在C#创建从byte []图像时参数无效错误 [英] Parameter is not valid error when creating image from byte[] in c#

查看:619
本文介绍了在C#创建从byte []图像时参数无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换字节[] 位图在C#。以下是代码:

I am trying to convert a byte[] to Bitmap in c#. Following is the code:

MemoryStream ms = new MemoryStream(b);
Bitmap bmp = new Bitmap(ms);



它显示误差参数无效创建位图时。

字节[] b 来自网络陆续建成。

但是,当我写一个文件这个字节[],并打开任意图形浏览该文件只是完美的作品。以下是代码编写字节[]到文件:

But when I write this byte[] to a file, and open this file in any image viewer just works perfectly. Following is code for writing the byte[] to file:

 var fs = new BinaryWriter(new FileStream("tmp.bmp", FileMode.Create, FileAccess.Write));
 fs.Write(b);
 fs.Close();



我缺少的是在这里吗?

What am I missing here?

修改

这里是我的,是造成问题

 Socket s = listener.AcceptSocket();
 byte[] b = new byte[imgLen];
 s.Receive(b);
 MemoryStream ms = new MemoryStream(b);
 // now here I am using ms.Seek(0, SeekOrigin.Begin); that fixed my problem.
 Bitmap bmp = new Bitmap(ms);
 pictureBox1.Image = bmp;
 s.Close();



我使用的Form_Load 这段代码事件并没有什么多余的。我只是想显示的网络流媒体图像。服务器是用Java编写的是流这一形象。

I am using this code on Form_Load event and there is nothing extra. I am just trying to display an Image that is streamed on network. The server is written in Java that is streaming this image.

希望它阐明了怀疑。

感谢

推荐答案

尝试在流重置当前位置

MemoryStream ms = new MemoryStream(b);
ms.Seek(0, SeekOrigin.Begin);
Bitmap bmp = new Bitmap(ms);

这篇关于在C#创建从byte []图像时参数无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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