抛出了类型'System.OutOfMemoryException'的异常。使用内存流时的C# [英] Exception of type 'System.OutOfMemoryException' was thrown. C# when using Memory stream

查看:346
本文介绍了抛出了类型'System.OutOfMemoryException'的异常。使用内存流时的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用wpf应用程序和内存流写入方法用于编写dicom数据字节。当尝试编写大小超过70 mb的大型dicom数据时,它显示类型'System.OutOfMemoryException'的异常。你能否提出任何解决方案来解决这个问题。

这段代码是这样的

 尝试 
{
使用(MemoryStream imagememoryStream = new MemoryStream ())
{
while true
{
// 检索DICOMData。
// 数据以块的形式出现;如果文件大小较大,则必须引发多个RetrieveDICOMData()调用
// 。返回值指定块是否是最后一个。
dicomData = dicomService.RetrieveDICOMData(hierarchyInfo);
imagememoryStream.Write(dicomData.DataBytes, 0 ,dicomData.DataBytes.Length);
if (dicomData.IsLastChunk)
{
// < span class =code-comment>数据较小;
// 已完成阅读,结束
断裂;
}
}
imageData = imagememoryStream.ToArray();
}
return imageData;
}
catch (异常异常)
{
throw new DataException(exception.StackTrace);
}



提前致谢。

你的忠实Sajitha

解决方案

< blockquote>在使用它之前刷新内存流...

 imagememoryStream.Flush(); 
imagememoryStream.Position = 0 ;


以小包装或小包装写入数据。这不会给出异常


  int  localBufferSize =  4  *  1024 ;  //   4 KB  
byte [] obuffer = new byte [localBufferSize];
while true
{
int size = fileStream.Read(obuffer, 0 ,localBufferSize);
if (size > 0
{
MemoryStream.Write(obuffer, 0 ,size);
}
其他
{
break ;
}
}







我在我的申请中使用了这段代码。希望它对你有帮助。


am working with wpf application and memory stream write method is used in that to write dicom data bytes.It shows the Exception of type 'System.OutOfMemoryException' when try to write big dicom data having size more than 70 mb. Can you please suggest any solution to resolve this.
The piece of code is like this

try 
{ 
using ( MemoryStream imagememoryStream = new MemoryStream())
 {
 while (true) 
{ 
// Retrieve the DICOMData. 
// data comes as chunks; if file size is larger, multiple RetrieveDICOMData() calls 
// has to be raised. the return value specifies whether the chunk is last one or not.
dicomData = dicomService.RetrieveDICOMData( hierarchyInfo ); 
imagememoryStream.Write( dicomData.DataBytes, 0, dicomData.DataBytes.Length );
if (dicomData.IsLastChunk) 
{ 
// data is smaller; 
//completed reading so, end 
break;
 }
 } 
imageData=imagememoryStream.ToArray(); 
} 
return imageData;
 } 
catch( Exception exception )
 { 
throw new DataException( exception.StackTrace );
 }


Thanks in advance.
Yours faithfully Sajitha

解决方案

Flush memory stream before using it...

imagememoryStream.Flush();
imagememoryStream.Position = 0;


Write data in small packages or small bunches . this would not give the exception


int localBufferSize = 4 * 1024;   // 4 KB
              byte[] obuffer = new byte[localBufferSize];
              while (true)
              {
                  int size = fileStream.Read(obuffer, 0, localBufferSize);
                  if (size > 0)
                  {
                      MemoryStream.Write(obuffer, 0, size);
                  }
                  else
                  {
                      break;
                  }
              }




I have used this code in my application. Hope it helps you.


这篇关于抛出了类型'System.OutOfMemoryException'的异常。使用内存流时的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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