Click here to Skip to main content
13,664,877 members
Rate this:
 
Please Sign up or sign in to vote.
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..
Posted 4-Feb-12 23:48pm
Updated 4-Feb-12 23:50pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 :)

Source: http://weblogs.asp.net/ashicmahtab/archive/2008/08/25/memorystream-not-expandable-invalid-operation-exception.aspx[^]
  Permalink  

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


Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.180810.1 | Last Updated 5 Feb 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100