从MemoryStream创建IMAGE对象时出现的参数无效?

山上的夜 2009-02-23 12:42:43
谁可以帮我看看


foreach (DataRow dr in ds.Tables[0].Rows)
{
MemoryStream ms = new MemoryStream();
byte[] img = (byte[])dr[5];
ms.Write(img, 0, img.Length);
MessageBox.Show(img.Length+""+ms.Length);
Image imge = Image.FromStream(ms);//这里说参数ms无效
dr[5] = imge;
ms.Close();
}

出现了参数无效的问题
...全文
2105 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
有没有解决啊,我也碰上了着额问题,蛋疼
机器人 2009-02-23
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070613/13/e74b7695-f5c2-4d82-b35f-5b31dc5f0da6.html
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 leaflet7758 的回复:]
引用 3 楼 fangxinggood 的回复:
Image imge = Image.FromStream(ms);//这里说参数ms无效

改为:

Image imge = new Bitmap(ms);


大哥,还是参数无效!
[/Quote]

是数据库中的IMAGE字段
机器人 2009-02-23
  • 打赏
  • 举报
回复
你存进去的Btye[]是什么格式?
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fangxinggood 的回复:]
Image imge = Image.FromStream(ms);//这里说参数ms无效

改为:

Image imge = new Bitmap(ms);
[/Quote]

大哥,还是参数无效!
机器人 2009-02-23
  • 打赏
  • 举报
回复
Image imge = Image.FromStream(ms);//这里说参数ms无效

改为:

Image imge = new Bitmap(ms);
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
大哥,DR[5]中的数据是二进制数据,我从数据库取出来的!
zgke 2009-02-23
  • 打赏
  • 举报
回复
你先确定 dr[5] 是图形格式的image才可以....
你可以把查看下img里到底是什么 ,可能不是.NETZ支持的IMAGE类型 或则数据已经损坏.
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
感谢各位帮助,谢谢啊
abcyzq 2009-02-23
  • 打赏
  • 举报
回复
SqlParameter sp_image = new SqlParameter("@add_image", SqlDbType.Image);
SqlDbType.Image类型,不要带长度16,我以前犯过这样的错,16是位。
机器人 2009-02-23
  • 打赏
  • 举报
回复
直接拼接字符串的方式,进到DB里格式不正确。
机器人 2009-02-23
  • 打赏
  • 举报
回复
完整调用:
String sql = "insert into Addressee values(@add_id,@add_name,@add_number,@add_num,@add_type,@add_image)"; 
SqlParameter sp_id = new SqlParameter("@add_id", SqlDbType.VarChar);
SqlParameter sp_name = new SqlParameter("@add_name", SqlDbType.VarChar);
SqlParameter sp_number = new SqlParameter("@add_number", SqlDbType.VarChar);
SqlParameter sp_num = new SqlParameter("@add_num", SqlDbType.VarChar);
SqlParameter sp_type = new SqlParameter("@add_type", SqlDbType.VarChar);
SqlParameter sp_image = new SqlParameter("@add_image", SqlDbType.Image);

sp_id.Value = vo.Id;
sp_name.Value = vo.Name;
sp_number.Value = vo.Number;
sp_num.Value = vo.Num;
sp_type.Value = vo.Type;
sp_image.Value = vo.Img;

List<SqlParamter> paramArray = new List<SqlParamter>();
paramArray.Add(sp_id);
paramArray.Add(sp_name);
paramArray.Add(sp_number);
paramArray.Add(sp_num);
paramArray.Add(sp_type);
paramArray.Add(sp_image);

int intResult = Execute(sql, paramArray);
-------------------------------------------------------------

public int Execute(String sql, List <SqlParamter> paramArray)
{
SqlConnection con = null;
try
{
con = Getcon();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
for (int i=0; i <paramArray.Length; i++)
{
cmd.Parameters.Add(paramArray[i]);
}
return cmd.ExecuteNonQuery();
}
finally
{
if(con != null)
con.Close();
}
}
机器人 2009-02-23
  • 打赏
  • 举报
回复
把你的插入语句修改为如下,再试一试:

String sql = "insert into Addressee values(@add_id,@add_name,@add_number,@add_num,@add_type,@add_image)";
SqlParameter sp_id = new SqlParameter("@add_id", SqlDbType.VarChar);
SqlParameter sp_name = new SqlParameter("@add_name", SqlDbType.VarChar);
SqlParameter sp_number = new SqlParameter("@add_number", SqlDbType.VarChar);
SqlParameter sp_num = new SqlParameter("@add_num", SqlDbType.VarChar);
SqlParameter sp_type = new SqlParameter("@add_type", SqlDbType.VarChar);
SqlParameter sp_image = new SqlParameter("@add_image", SqlDbType.Image);

sp_id.Value = vo.Id;
sp_name.Value = vo.Name;
sp_number.Value = vo.Number;
sp_num.Value = vo.Num;
sp_type.Value = vo.Type;
sp_image.Value = vo.Img;

public int Execute(String sql, List<SqlParamter> Parameters)
{
SqlConnection con = null;
try
{
con = Getcon();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
for (int i=0; i<paramArray.Length; i++)
{
cmd.Parameters.Add(paramArray[i]);
}
return cmd.ExecuteNonQuery();
}
finally
{
if(con != null)
con.Close();
}
}
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
-----数据库字段
create table Addressee
(
id varchar(20) not null constraint PK_ID primary key(id),
cardName varchar(100),
number varchar(50),
num varchar(50),
AddresseetType varchar(50),
img image
)

------插入代码
String sql = "insert into Addressee values('"+vo.Id+"','"+vo.Name+"','"+vo.Number+"','"+vo.Num+"','"+vo.Type+"','"+vo.Img+"')";vo是一个实体类
---执行代码
public int Execute(String sql)
{
SqlConnection con = Getcon();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteNonQuery();
}
机器人 2009-02-23
  • 打赏
  • 举报
回复
插入数据库的代码贴出来看看,另外插入之后你用IDE的DB Browser能正常浏览插入之后的图片吗?
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 fangxinggood 的回复:]
lz,BinaryFormatter没用吧。

FileStream fs = new FileStream(ofdImg.FileName, FileMode.Open, FileAccess.Read); <-- OK
BinaryFormatter bf = new BinaryFormatter(); <-- OK
BinaryReader rd = new BinaryReader(fs); <-- OK

你Image字段用的是什么数据库的什么类型?
[/Quote]
image类型的
机器人 2009-02-23
  • 打赏
  • 举报
回复
lz,BinaryFormatter没用吧。

FileStream fs = new FileStream(ofdImg.FileName, FileMode.Open, FileAccess.Read); <-- OK
BinaryFormatter bf = new BinaryFormatter(); <-- OK
BinaryReader rd = new BinaryReader(fs); <-- OK

你Image字段用的是什么数据库的什么类型?
山上的夜 2009-02-23
  • 打赏
  • 举报
回复
图像原来的是这样被我转换的
FileStream fs = new FileStream(ofdImg.FileName, FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
BinaryReader rd = new BinaryReader(fs);
int len = Convert.ToInt32(fs.Length);
byte[] img = new byte[len];
img = rd.ReadBytes(len);
----------------
取出来的时候我用了适配器讲述填充到DATASET中,
然后我想讲图像还原
-------我的还原代码

foreach (DataRow dr in ds.Tables[0].Rows)
{
MemoryStream ms = new MemoryStream();
byte[] img = (byte[])dr[5];
ms.Write(img, 0, img.Length);
MessageBox.Show(img.Length+""+ms.Length);
Image imge = Image.FromStream(ms);//这里说参数ms无效
dr[5] = imge;
ms.Close();
}

我都试用了各位热心人的方法,可能自己笨了点,还是不行啊!!!

还是
异常信息
未处理 System.ArgumentException
Message="参数无效。"
Source="System.Drawing"
StackTrace:
在 System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
在 System.Drawing.Image.FromStream(Stream stream)
在 WindowsApplication1.FrmDataDetail.ImageChange() 位置 D:\MyFC\MyFCSLN\WindowsApplication1\FrmDataDetail.cs:行号 89
在 WindowsApplication1.FrmDataDetail.GetAlldate() 位置 D:\MyFC\MyFCSLN\WindowsApplication1\FrmDataDetail.cs:行号 77
在 WindowsApplication1.FrmDataDetail.FrmDataDetail_Load(Object sender, EventArgs e) 位置 D:\MyFC\MyFCSLN\WindowsApplication1\FrmDataDetail.cs:行号 31
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsApplication1.Program.Main() 位置 D:\MyFC\MyFCSLN\WindowsApplication1\Program.cs:行号 17
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
北京的雾霾天 2009-02-23
  • 打赏
  • 举报
回复
你可能需要ImageConverter 类来转换,因为数据库里存放的图片格式可能较早,而这个类可以很好的解决这个问题,如下:


if (bts is byte[])
{
ImageConverter imc = new ImageConverter();
Image _img = imc.ConvertFrom(null, null, bts) as Image;
if (_img != null)
{
tmpObj = _img;
}
}
longhui666888 2009-02-23
  • 打赏
  • 举报
回复
把ms.Position = 0;插到 ms.Write(img, 0, img.Length);
后面试试

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧