locked
Parameter is not valid. memory stream error RRS feed

  • Question

  • User-2010772455 posted

    Hi,  I have these 2 functions that save an image using a stream, it works with small images (1kb) but not with large ones.

    and i get the error:

    Parameter is not valid.

    pointing:

    Line 202: long len = ms.Length;

    Line 203: byte[] b = new byte[len];

    Line 204: using (System.Drawing.Image image = System.Drawing.Image.FromStream(ms))

     Line 205: {

    Line 206: image.Save(Path.Combine(@"C:\Trash\", fileName));

     

            public static string writeImage(string imageLink)
            {
                string fileName = getFileName(imageLink);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(imageLink);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    
                using (Stream st = resp.GetResponseStream())
                {
    
                    MemoryStream ms = (MemoryStream)CopyStream(st);
                    ms.Seek(0, SeekOrigin.Begin);
                    long len = ms.Length;
                    byte[] b = new byte[len];
                    using (System.Drawing.Image image = System.Drawing.Image.FromStream(ms))
                    {
                        image.Save(Path.Combine(@"C:\Trash\", fileName));
                    }
                }
                resp.Close();
                return fileName;
            }
    
            public static Stream CopyStream(Stream inputStream)
            {
                const int readSize = 1048576;
                byte[] buffer = new byte[readSize];
                MemoryStream ms = new MemoryStream();
                int count = inputStream.Read(buffer, 0, readSize);
                while (count > 0)
                {
                    ms.Write(buffer, 0, count);
                    count = inputStream.Read(buffer, 0, readSize);
                }
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }

    and in webconfig i have this(in <system.web>:

    <httpRuntime maxRequestLength="5000" requestLengthDiskThreshold="1536" />

    the image i'm trying to save is 137740 bytes long

    I've searched google and everyone say it's either the pointer not pointing to the begining (which it is) or httpRuntime configuration, which is set to a much bigger value.

    what am I missing here?

    Saturday, May 7, 2011 12:27 PM

Answers