功能就是简单的两个picturebox 把第一个box里面的图片序列化成流存到数据库中 字段类型为image 通过点击按钮把数据库中的图片反序列化出来放到第二个box里面 出现下面错误:报错为:在分析完成之前就遇到流结尾 跟踪的时候在序列化的时候加断点看到里面有数据 在反序列化的时候看到也有数据期待大家帮忙,先谢谢了代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
WhiteImage(this.pictureBox1);
}
public void ReadImage(PictureBox pb1)
{ SqlConnection scon = new SqlConnection("data source=.;database=thingking;uid=sa;pwd=sa"); scon.Open();
string sql = "select imagee from im where ws='88'"; SqlCommand cmd = new SqlCommand(sql, scon);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
byte[] bytes1 = (byte[])dr["imagee"];
pb1.Image = ConvertImage(bytes1);
}
dr.Close();
scon.Close();
} public int WhiteImage( PictureBox pb1)
{
SqlConnection scon = new SqlConnection("data source=.;database=thingking;uid=sa;pwd=sa");
scon.Open();
string sql="insert into im values('"+ConvertByte(this.pictureBox1.Image)+"','88')";
SqlCommand com = new SqlCommand(sql, scon);
com.ExecuteNonQuery();
return 1; }
private Image ConvertImage(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 byte[] ConvertByte(Image image)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, (object)image);
ms.Close();
return ms.ToArray();
} private void button2_Click(object sender, EventArgs e)
{
ReadImage(this.pictureBox2);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
WhiteImage(this.pictureBox1);
}
public void ReadImage(PictureBox pb1)
{ SqlConnection scon = new SqlConnection("data source=.;database=thingking;uid=sa;pwd=sa"); scon.Open();
string sql = "select imagee from im where ws='88'"; SqlCommand cmd = new SqlCommand(sql, scon);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
byte[] bytes1 = (byte[])dr["imagee"];
pb1.Image = ConvertImage(bytes1);
}
dr.Close();
scon.Close();
} public int WhiteImage( PictureBox pb1)
{
SqlConnection scon = new SqlConnection("data source=.;database=thingking;uid=sa;pwd=sa");
scon.Open();
string sql="insert into im values('"+ConvertByte(this.pictureBox1.Image)+"','88')";
SqlCommand com = new SqlCommand(sql, scon);
com.ExecuteNonQuery();
return 1; }
private Image ConvertImage(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 byte[] ConvertByte(Image image)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, (object)image);
ms.Close();
return ms.ToArray();
} private void button2_Click(object sender, EventArgs e)
{
ReadImage(this.pictureBox2);
}
}
}
把你以前保存的数据都删除了把...
然后使用 SqlParameter来保存. public int WhiteImage(PictureBox pb1)
{
SqlConnection scon = new SqlConnection("data source=.;database=thingking;uid=sa;pwd=sa");
scon.Open();
string sql = "insert into im values(@SaveImage),'88')";
SqlCommand com = new SqlCommand(sql, scon);
com.Parameters.Add(new SqlParameter("@SaveImage", SqlDbType.Image));
com.Parameters[0].Value = ConvertByte(this.pictureBox1.Image);
com.ExecuteNonQuery();
return 1;
}
byte[] imgBytes = null;
if (this.pictureBox1.Image == null)
{
imgBytes = new byte[] { };
}
else
{
MemoryStream mStream = new MemoryStream();
this.pictureBox1.Image.Save(mStream, ImageFormat.Jpeg);
mStream.Position = 0;
imgBytes = new byte[mStream.Length];
mStream.Read(imgBytes, 0, imgBytes.Length);
}楼主保存的时候使用参数的形式将imgBytes传进去,格式为SqlDbType.Image
数据库对应字段为image类型,读取的时候,将该字段值转成byte[]格式Bitmap bmp = null;
byte[] imgBytes = (byte[])dr["image"];
if (imgBytes != null && imgBytes.Length != 0)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
stream.Write(imgBytes, 0, imgBytes.Length);
stream.Position = 0;
bmp = System.Drawing.Image.FromStream(stream);
}
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, (object)image);
//ms.Close(); 这里关闭不合适;
ms.Position = 0;//加上这一句,重新复位到开始位置。
return ms.ToArray();
}