抛出了类型'System.OutOfMemoryException'的异常。使用内存流时的C#

时间:2013-08-08 04:30:59

标签: c# dicom memorystream large-data

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

这段代码就像这样

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 );
            }

1 个答案:

答案 0 :(得分:1)

由于缺少连续(非全部)可用内存,MemoryStream抛出OutOfMemoryExceptions是很常见的。有许多替代实现可以减轻这个问题。例如,查看MemoryTributary

或者,根据您的需要,您可以尝试直接写入存储而不是内存。