1

I'm trying to convert image to byte array but it crashes the application.

The error is something like this:

There was an error deserializing the object. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader

This is my code:

if (dr["Photo"] != DBNull.Value)
{
    rider.Photo = (byte[])dr["Photo"];
}

Is there a better way to do this? Something that works?

Edit: So, I believe it's something to do with image size being too large for it to pass through the web service. I edited the webconfig files and changed the ReaderQuotes tag values like this:

<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

Even then, the output is same.

  • Your error message is about XML not SQL. Please show the code that the exception is referring to. – Richard Schneider Sep 28 '15 at 07:35
  • the application is crashing on the very line where I'm converting Photo to byte array. –  Sep 28 '15 at 07:41
  • I really do not believe the error message you show is associated with code. I'm assuming that `dr` is a SQL `DataReader` of some type. – Richard Schneider Sep 28 '15 at 07:44
  • I edited MaxArrayLength properties in web.config files. Put a big number like maxArrayLength="2147483647". But it still crashes. One thing I should've mentioned is that it's a wcf application. –  Sep 28 '15 at 07:54

1 Answers1

0

This would do the trick

if (dr["Photo"] != DBNull.Value)
{
    Byte[] data = (Byte[])(dr["Photo"]);
    MemoryStream mem = new MemoryStream(data);
    rider.Photo = Image.FromStream(mem);
}

Whereas I believe instead of .Photo it should be .Image

You can have a look on The maximum array length quota (16384) has been exceeded

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Nope @TahreemIqbal it should be `System.Drawing` namespace instead of `System.Net.Mime.MediaTypeNames` you can specify if there is any ambiguity in the code `System.Drawing.Image.FromStream(mem);` – Mohit S Sep 28 '15 at 07:30