1

I have done this one for converting the byte of array in one of the columns in gridview to system.drawing.image got an error at this line

Image returnImage = Image.FromStream(ms);        

              argumentexception was unhandled 
              Parameter is not valid.

and the code is like this

private byte[] objtoarray(object obj)
{         
    if (obj == null)
        return null;

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);

    return ms.ToArray();

}

public Image bytearraytoimage(byte[] bytearray)
{
    MemoryStream ms = new MemoryStream(bytearray,0,bytearray.Length);

    Image returnImage = Image.FromStream(ms);
    return returnImage;

}

private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
{
    byte[] bits = null;
    Image img = null;

    if (e.ColumnIndex != productgridview.Columns["productimage"].Index) return;

    if (productgridview.SelectedCells.Count == 0) return;

    object selectedValue = productgridview.SelectedCells[0].Value;
    bits= objtoarray(selectedValue);
    img = bytearraytoimage(bits);

    if (img is Image)
    {
        // Forms are IDisposable, so use them embedded in a using statement.
        using (ProductDescriptionForm pf = new ProductDescriptionForm())
        {
            pf.picture = (Image)selectedValue;
            pf.ShowDialog(this);
        }
    }      
}
Jason Down
  • 21,323
  • 12
  • 82
  • 116
Glory Raj
  • 17,092
  • 26
  • 94
  • 188

1 Answers1

0

The docs state that ArgumentException can be raise for :

The stream does not have a valid image format
-or-
stream is null.

So since objtoarray can return null, is productgridview.SelectedCells[0].Value null, or of an invalid value?

    object selectedValue = productgridview.SelectedCells[0].Value;
    bits= objtoarray(selectedValue);
Preet Sangha
  • 62,844
  • 17
  • 138
  • 209
  • Image returnImage = Image.FromStream(ms); i have seen the value for selectedValue is like this "byte[40678]" – Glory Raj Aug 18 '11 at 10:56
  • i got the value at this statement like this object selectedValue = productgridview.SelectedCells[0].Value; after this statement the seleceted value is like this byte[44899].... am i doing any thing wrong .... – Glory Raj Aug 18 '11 at 11:05
  • thanq peter the data coming from grid view is also byte of arrays so no need to convert again into byte of arrays ....i made a mistake ..now its working fine..... – Glory Raj Aug 18 '11 at 11:08