Click here to Skip to main content
15,305,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
Posted
Updated 7-Aug-13 18:46pm
v2

Flush memory stream before using it...
C#
imagememoryStream.Flush();
imagememoryStream.Position = 0;
   
Write data in small packages or small bunches . this would not give the exception
   
C#
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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900