简体   繁体   English

要从数据库检索数据

[英]Want to retrieve data from database

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;

I face problem when we hit the line Image oImage = Image.FromStream((Stream)ms); 当我们点击Image oImage = Image.FromStream((Stream)ms);时,我会遇到问题Image oImage = Image.FromStream((Stream)ms); this line is executed, but afterwards I get an exception "Parameter is not valid." 这行被执行,但是之后我得到一个异常“参数无效”。

I'm guessing the imgData array doesn't actually contain a valid image (or one that Image.FromStream() understands at least). 我猜imgData数组实际上没有包含有效的图像(或Image.FromStream()至少可以理解的图像)。

Try checking the data against what you think it should be. 尝试根据您的预期检查数据。 You can also try saving the stream to a file and opening it that way - I'm guessing it will fail as "invalid format". 您也可以尝试将流保存到文件并以这种方式打开-我猜它会因为“无效格式”而失败。 If it opens correctly, have a look at this related question . 如果打开正确,请查看此相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM