字节[]到图像 [英] Byte[] to Image

查看:81
本文介绍了字节[]到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想将byte []转换为Image。我在新闻组中读到

最好的方法是使用MemoryStream。我试过了,但是在创建图像时总是得到一个

异常(类型为

的未处理异常'System.ArgumentException''发生在system.drawing.dll中。附加

信息:Ungültiger参数verwendet)。这是我的代码:


Image newImage;

using(MemoryStream ms = new MemoryStream(length))

{

byte [] bArray = new byte [length];

for(int k = 0; k< length; k ++)

{

bArray [k] = 10;

}

ms.Write(bArray,0,length);

newImage = Image.FromStream(ms);

}

}


感谢您的帮助!

Arno

解决方案

在将流传递给Image

构造函数之前,必须先设置流的位置。

ms.Position = 0;


" Arno" <き****** @ gmx.de>在消息新闻中写道:e0 ********** @ online.de ...



我想将一个byte []转换为一个图像。我在新闻组中读到,最好的方法是使用MemoryStream。我试过了,但是在创建Image时我总是得到一个例外(在system.drawing.dll中发生了一个未处理的类型
''System.ArgumentException''例外。附加
信息: Ungültiger参数verwendet)。这是我的代码:

图像newImage;
使用(MemoryStream ms = new MemoryStream(length))
{
byte [] bArray = new byte [length] ;
for(int k = 0; k< length; k ++)
{
bArray [k] = 10;
}
ms.Write(bArray,0 ,长度);
newImage = Image.FromStream(ms);
}

感谢您的帮助!
Arno



Lebesgue< no **** @ spam.jp>写道:

你必须先设置流的位置,然后再将其传递给Image
构造函数。
ms.Position = 0;




在这种情况下你还必须*不*使用using语句 - 否则
否则流将自动关闭。 Image.FromStream

要求流保持打开状态,直到*它*想关闭它。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

如果回复小组,请不要给我发邮件




感谢您的帮助到目前为止...我做了更改,您(两个)告诉我,

但我仍然得到相同的异常(System.ArgumentException)。 ..

我的代码现在看起来像这样:

Image newImage;

MemoryStream ms = new MemoryStream(length);

byte [] bArray = new byte [length];

for(int k = 0; k< length; k ++)

{

bArray [k] = 10;

}

ms.Write(bArray,0,length);

ms .Position = 0;

新Image = Image.FromStream(ms);


谢谢,

Arno

Lebesgue<否**** @ spam.jp>写道:

你必须先设置流的位置,然后再将其传递给Image
构造函数。
ms.Position = 0;


在这种情况下,您还必须*不*使用using语句 - 否则,流将自动关闭。 Image.FromStream
要求流保持打开,直到*它*想关闭它。



Hi,

I want to convert a byte[] to a Image. I read in a newsgroup that the
best way is to use a MemoryStream. I tried it, but I get always a
Exception while creating the Image(An unhandled exception of type
''System.ArgumentException'' occurred in system.drawing.dll. Additional
information: Ungültiger Parameter verwendet). Here is my code:

Image newImage;
using (MemoryStream ms = new MemoryStream(length))
{
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
newImage = Image.FromStream(ms);
}
}

Thanks for your help !
Arno

解决方案

You have to set stream''s position to start before passing it to the Image
constructor.
ms.Position = 0;

"Arno" <ki******@gmx.de> wrote in message news:e0**********@online.de...

Hi,

I want to convert a byte[] to a Image. I read in a newsgroup that the best
way is to use a MemoryStream. I tried it, but I get always a Exception
while creating the Image(An unhandled exception of type
''System.ArgumentException'' occurred in system.drawing.dll. Additional
information: Ungültiger Parameter verwendet). Here is my code:

Image newImage;
using (MemoryStream ms = new MemoryStream(length))
{
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
newImage = Image.FromStream(ms);
}
}

Thanks for your help !
Arno



Lebesgue <no****@spam.jp> wrote:

You have to set stream''s position to start before passing it to the Image
constructor.
ms.Position = 0;



You also have to *not* use a using statement in this case - as
otherwise the stream will be closed automatically. Image.FromStream
requires the stream to stay open until *it* wants to close it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Hi,

thanks for your help so far ... I made the changes you (both) told me,
but I still get the same Exception (System.ArgumentException) ...
My code now looks like this:

Image newImage;
MemoryStream ms = new MemoryStream(length);
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
ms.Position = 0;
newImage = Image.FromStream(ms);

thanks,
Arno

Lebesgue <no****@spam.jp> wrote:

You have to set stream''s position to start before passing it to the Image
constructor.
ms.Position = 0;


You also have to *not* use a using statement in this case - as
otherwise the stream will be closed automatically. Image.FromStream
requires the stream to stay open until *it* wants to close it.



这篇关于字节[]到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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