tags:

views:

100

answers:

2
+1  Q: 

Image Retrive

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 create problem when "Image oImage = Image.FromStream((Stream)ms);" line execute.....this line show "Parameter is not valid" message .......why it's occur 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 occur when this line execute

+1  A: 

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;
Greco
A: 

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:

  • Write the byte data to a file and try to open the image in a graphics program (to verify that the byte data does indeed represent a valid image). My guess is that this would fail.
  • Check the code that writes the data to the database (perhaps perform the same trick as in the previous point; write it to a file and try to open the file)
Fredrik Mörk