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 bytearra开发者_如何学运维ytoimage(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);
}
}
}
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);
精彩评论