Image image = pictureEdit1.Image;//从第三方控件中取得Image对象
MemoryStream ms = new MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);byte[] writeIn = new byte[ms.Length];ms.Read(writeIn,0,(int)ms.Length);//这里查看writeIn发现全部为0???!!SqlConnection conn = new SqlConnection("Data Source=Localhost;User ID=sa;Password=xxxx;Initial Catalog=forwarding.net");try
{
conn.Open();SqlCommand comm = new SqlCommand("insert into imagestorage(image) values(@image)",conn);
comm.Parameters.Add("@image",writeIn);comm.ExecuteNonQuery();conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}上面这段代码问题在哪?ms.Length是long类型的,但ms.Read(writeIn,0,(int)ms.Length)中取的时候却一定要int问题是不是出在这里?如果思路不是这样,请问应该如何写?
MemoryStream ms = new MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);byte[] writeIn = new byte[ms.Length];ms.Read(writeIn,0,(int)ms.Length);//这里查看writeIn发现全部为0???!!SqlConnection conn = new SqlConnection("Data Source=Localhost;User ID=sa;Password=xxxx;Initial Catalog=forwarding.net");try
{
conn.Open();SqlCommand comm = new SqlCommand("insert into imagestorage(image) values(@image)",conn);
comm.Parameters.Add("@image",writeIn);comm.ExecuteNonQuery();conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}上面这段代码问题在哪?ms.Length是long类型的,但ms.Read(writeIn,0,(int)ms.Length)中取的时候却一定要int问题是不是出在这里?如果思路不是这样,请问应该如何写?
sql="insert into p_ry_photo(id,photo) values ('"+id+"',@i)";
SqlCommand cmd=new SqlCommand(sql,CommonVar.SqlConDataBase);
MemoryStream s = new MemoryStream();
picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = s.ToArray();
s.Close();
cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);
cmd.Parameters["@i"].Value=b;
cmd.ExecuteNonQuery();
{
try
{ //读取图象
string ls_sql="select photo from p_ry_photo where id='"+id+"'";
this.picPhoto.Image=null;
this.picPhoto.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,CommonVar.SqlConDataBase);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
byte[] b = (byte[])reader[0];
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
picPhoto.Image = image;
s.Close();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但为什么用ms.Read(writeIn,0,(int)ms.Length)会导致byte数组writeIn全部是0,这个方法不是把ms的内容复制到byte数组吗??