想要从数据库中检索数据

时间:2009-05-20 09:22:09

标签: c# .net

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;

当我们点击行Image oImage = Image.FromStream((Stream)ms);执行此行时遇到问题,但之后我收到异常“参数无效。”

1 个答案:

答案 0 :(得分:1)

我猜测imgData数组实际上并不包含有效图像(或Image.FromStream()至少理解的图像。)

尝试根据您的想法检查数据。您也可以尝试将流保存到文件并以这种方式打开 - 我猜它会因为“格式无效”而失败。如果打开正确,请查看this related question