2011-12-13 79 views
3

我得到这个错误ReadTimeout转换字节数组到流

ReadTimeout = '((System.IO.Stream)(ms)).ReadTimeout' threw an exception of type 'System.InvalidOperationException'. 

我的代码时引发了异常。

byte[] imageBytes = Convert.FromBase64String(img); 
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);  //Problem generates here// 

谢谢..

+0

唯一的例外是不是在您指定的行抛出。 ((System.IO.Stream)(ms))。ReadTimeout'抛出一个异常... –

+1

只有在调试时才会发生异常吗?它会抛出一个异常,因为'MemoryStream'不支持超时,但它不应该影响你的程序(根据你发布的两行代码)。 –

回答

-1

ReadTimeout属性必须被覆盖,在基System.IO.Stream类它总是抛出System.InvalidOperationException错误by design

int readTimeout = ms.ReadTimeout; 

编辑:发布前没有检查.. MemoryStream的也不会覆盖财产

读取超时时,该解决方案是不是要投 ms基础类型 - 这种流的意思超时未实现。

您必须使用Stream类的其他实现来覆盖ReadTimeout属性,或者编写自己的实现。

+0

如果Mia建议在'MemoryStream(byte [],int,int)'构造函数中确实发生异常,那么这不是一个框架错误,当然不是设计?此外,我认为即使将其投射到基地,“虚拟”方法也会在实际课程中结束? –

+0

当我运行我得到'参数无效。'我QuickWatched我的流ms,并从我得出的结论---- ReadTimeout ='((System.IO.Stream)(ms))。ReadTimeout'抛出类型'System.InvalidOperationException'的异常和WriteTimeOut =' ((System.IO.Stream)(ms))。ReadTimeout'抛出一个异常'System.InvalidOperationException' – Mia

+0

@ C.Evenhuis没有这个错误不能来自构造函数'MemoryStream'我只是再次检查其代码。 –

4

仅当流支持超时时,才可以使用ReadTimeout/WriteTimeout属性。 您可以通过CanTimeout属性进行检查。如果CanTimeout属性返回false,则对这些属性的所有访问应该引发InvalidOperationException。

-2

你应该小心数据库中的图像类型,如果图像太大,那么你必须使用LONGBLOB而不是BLOB。

+1

LONGBLOB是一个MySQL的东西。这个问题与数据库无关。 –

0

我用下面的代码,我需要的MemoryStream用作电子邮件附件:

string [email protected]"C:\images\myimage.img" 
MemoryStream result = new MemoryStream(); 
MemoryStream source = new MemoryStream(File.ReadAllBytes(filename)); 
source.WriteTo(result);