Reputation: 383
byte[] imageData = null;
long byteSize = 0;
byteSize = _reader.GetBytes(_reader.GetOrdinal(sFieldName), 0, null, 0, 0);
imageData = new byte[byteSize];
long bytesread = 0;
int curpos = 0, chunkSize = 500;
while (bytesread < byteSize)
{
// chunkSize is an arbitrary application defined value
bytesread += _reader.GetBytes(_reader.GetOrdinal(sFieldName), curpos, imageData, curpos, chunkSize);
curpos += chunkSize;
}
byte[] imgData = imageData;
MemoryStream ms = new MemoryStream(imgData);
Image oImage = Image.FromStream((Stream)ms);
return oImage;
Code creates problem when "Image oImage = Image.FromStream((Stream)ms);"
line executes.....This line shows "Parameter is not valid"
message .......Why it occurs? Help me. I want to retrieve image from database ....I work on C# window vs05 .....Can any one help me? byte[] contain value. All works well, just problem occurs when this line executes.
Upvotes: 0
Views: 295
Reputation: 158309
I can't really spot any errors in this code (other than the MemoryStream not being disposed, and that it's not necessary to cast it to Stream
when passing it to the Image.FromStream
method; but those should not cause your error). I would do the following in order to try to find the error:
Upvotes: 0
Reputation: 4949
A simple if statement should solve your problem before creating the memory stream
if (imageData.Length != 0)
{
MemoryStream ms = new MemoryStream(imageData);
Image oImage = Image.FromStream((Stream)ms);
return oImage;
}
return null;
Upvotes: 1