viewing photo from MySQL RRS feed

  • Question

  • Hi,

    I am trying to view a photo blob from MySQL on a picture control using the following code:

    byte[] rawData = new byte[0];
    rawData = (byte[])sql_reader["photo"];
    int len = new int();
    len = rawData.GetUpperBound(0);
    FileStream fs = new FileStream(photo_file, FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(rawData, 0, len);
    curImage = Image.FromFile(photo_file);
    pictureEmployee.Image = curImage;

    but getting the following error:

    Non-negative number required. Parameter name : count

    Wednesday, September 3, 2014 10:58 AM


  • Probably GetUpperBound is -1, meaning that the array is empty, i.e. there is no image. To get the correct length, use len = rawData.Length. It will be zero for empty arrays. You can check this using Debugger. Then clarify why there is no data.

    To avoid intermediate photo_file, you can make a MemoryStream from rawData, then use it in FromStream. Or see if sql_reader is able to return a stream.

    Wednesday, September 3, 2014 5:22 PM