使用C#检索图像 [英] Image Retrieve using c#

查看:61
本文介绍了使用C#检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);" 行执行时,

代码会产生问题...此行显示参数无效" 消息.......为什么发生?帮我.我想从数据库中检索图像..我在C#窗口vs05上工作...任何人都可以帮我吗?byte []包含值.一切正常,执行此行时只会出现问题.

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.

推荐答案

一个简单的if语句应该在创建内存流之前解决您的问题

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;

这篇关于使用C#检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆