代码如下,请各位知道的,帮忙说下什么错了,谢谢
public Image ReadImage(byte[] bytes)
{
MemoryStream ms=new MemoryStream(bytes,0,bytes.Length);
BinaryFormatter bf = new BinaryFormatter();
object obj=bf.Deserialize(ms);
ms.Close();
return (Image)obj;
}
//图片加载
private void ReadPicture(string FoodCode)
{
try
{
//读取图象
string ls_sql="select FoodPicture from FoodDetails where FoodCode='"+FoodCode+"'";
SqlConnection conn=new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open();
this.pictureBox1.Image=null;
this.pictureBox1.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,conn);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
if(reader.IsDBNull(1))
{
byte[] b = (byte[])reader["FoodPicture"];
this.pictureBox1.Image=this.ReadImage(b); }
}
reader.Close();
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
运行的报错说:索引超出数组了,不知道是什么原因,还请各位帮忙看看
public Image ReadImage(byte[] bytes)
{
MemoryStream ms=new MemoryStream(bytes,0,bytes.Length);
BinaryFormatter bf = new BinaryFormatter();
object obj=bf.Deserialize(ms);
ms.Close();
return (Image)obj;
}
//图片加载
private void ReadPicture(string FoodCode)
{
try
{
//读取图象
string ls_sql="select FoodPicture from FoodDetails where FoodCode='"+FoodCode+"'";
SqlConnection conn=new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open();
this.pictureBox1.Image=null;
this.pictureBox1.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,conn);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
if(reader.IsDBNull(1))
{
byte[] b = (byte[])reader["FoodPicture"];
this.pictureBox1.Image=this.ReadImage(b); }
}
reader.Close();
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
运行的报错说:索引超出数组了,不知道是什么原因,还请各位帮忙看看
解决方案 »
- 个人通讯录源码
- 如何记住Treeview哪个节点曾经展开过?
- winform 打印 string 是这样的,我想为公司打印EMS的客户单,把名字对准就行了
- 初学C# 一个简单加密字符串的程序,看看能不能更简单些
- 简单的问题
- .net ,C# ,我想在Web页面动态生成流程图,有这方面的控件或方法吗,
- 请教:在C#中怎么把1-30带圆圈的数字,即①②③④⑤⑥⑦⑧⑨⑩。。。。。
- C#写了一个程序,需要用到Windows+Enter热键
- 用DataSet取出数据后,有什么办法能够得到字段在SQL server 中原有的数据类型阿?????
- 怎样在C#用WebBrowser显示内存中的一个文件
- TableLogonInfo值不能为空,用水晶报表,前边查询FILL他的XSD数据集,同时显示GRID;然后在另外一个按钮中打印,怎么取不出数据来啊?
- 求一本电子书《VS.NET UML 建模高级编程》
-----------
//把图片读到pictureBox,再写入数据库SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();-------------------------------------------------------
//从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
public byte[] GetPictureSize(string filePath)
{
//读取文件的字节数,并返回字节数
byte[] imageData=null;
FileStream fs=new FileStream(filePath,FileMode.Open);//建立文件流
BinaryReader bread=new BinaryReader(fs);//建立二进制类
imageData=bread.ReadBytes(MAX_SIZE);//读取的字节数
return imageData;
}
public static byte[] ConvertImage(Image image)//将图像转化为字节
{
MemoryStream ms=new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms,(object)image);
ms.Close();
return ms.ToArray();
}
通过浏览选择图片到picturebox,然后利用ConvertImage将该图片转化为byte
我设置的MAX_SIZE值为60000
参考 BearRui(孤熊 | 带你去看海!) 的示例,加上FileAccess.Read 后还是运行报错:索引超出数组界限了,不知道是什么原因,还请各位帮忙看看