想要从数据库检索数据

问题描述:

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);这条线被执行,但后来我得到一个异常“参数无效”。想要从数据库检索数据

+0

也许向我们展示实际引发异常及其周围代码的行是有意义的。 IOW调用迄今为止向我们展示的函数的代码。 – AnthonyWJones 2009-05-20 09:32:36

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

尝试检查数据与您认为应该是什么。你也可以尝试将流保存到一个文件并以这种方式打开它 - 我猜它会失败为“无效格式”。如果打开正确,请看this related question