Click here to Skip to main content
15,885,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i have a code to read the image from sqldatabase like.

Byte[] imagedata = new Byte[0];

              imagedata = (byte[])dr[45];
              System.Drawing.Image b = null;
              MemoryStream mm = new MemoryStream(imagedata);
              byte[] buffer = new byte[4096];
              int byteseq = mm.Read(imagedata, 0, 4096);
              while (byteseq > 0)
                  //mm.Write(buffer, 0, byteseq);
                 // int byteseq = mm.Read(imagedata, 0,4096);
                  mm.Write(buffer, 0, buffer.Length);// Getting the error memory stream can not be expendable

can u please help me to solve this error..
Updated 4-Feb-12 23:50pm

1 solution

A simple Google search found this...

There's a little gotcha with the MemoryStream class that I just found out. It has 7 constructors. The default constructor has the stream set as expandable, with an initial capacity of 0. The ctors that take the capacity set the capacity to the param, but also keeps the stream expandable. If, however, you use a ctor with the byte[] param, it initializes the stream with the contents of the buffer, but the stream becomes non-expandable.

So, if you have something like this:

byte[] buffer = File.ReadAllBytes("filaname.docx");
MemoryStream ms = new MemoryStream(buffer);
MemoryStream ms2 = new MemoryStream();
ms2.Write(buffer, 0, buffer.Length);

then, ms will NOT be expandable, ms2 will be. So, if you are modifying the stream and the size may increase, the first ctor will not work, but the second approach will work just fine.

Hope that helps :)

Share this answer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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