111,130
社区成员
发帖
与我相关
我的任务
分享 ds=c.Show_photo("1");//这句是将图片放在dataset中
byte[] pic =(byte[])ds.Tables[0].Rows[0].ItemArray[0];//取出图片并转换为byte[]数组
ShowData_Image(pic, pictureBox1);//调用方法,方法内容看下面
public void ShowData_Image(byte[] DI, PictureBox Ima) //方法在这里,显示数据库图片
{
byte[] buffer = DI;//这里就是上面的pic
MemoryStream ms = new MemoryStream(buffer);
Ima.Image = Image.FromStream(ms);//????运行到这里出错了,说是参数无效
}问题在这:
C# codepic = (byte[])ds.Tables[0].Rows[0].ItemArray[3];
通过一个数组来获取或设置此行的所有值!而不是,这行这列的值!
应该这样:
C# codepic=(byte[])ds.Tables[0].Rows[0]["图片列的列名"].ToString();
ToString()的没这么写过,不知道行不行。
给你个例子参考下:
C# codeprivate void button1_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数所
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from tb_01", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(1);
}
dr.Close();
com.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pictureBox1.Image = bmpt;
}MemoryStream ms =new MemoryStream(buffer);
Bitmap bmp=new Bitmap(ms);
pictureBox.Image=bmp;public string SaveImage(string id, byte[] p)//这个是将图片存进数据库的方法
{
DataBase db = new DataBase();//连接数据库
try
{
SqlParameter[] ps ={
db.MakeInParam("@id",SqlDbType.VarChar,5,id),
db.MakeInParam("@photo",SqlDbType.Binary,10000,p)//就是这里,本来这个10000的值是16,现在改10000就好了,原来是太小了不能显示
};
db.RunProc("Save_Image", ps);
return "0";
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
return null;
}
finally
{
db.Close();
db = null;
}System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=.;uid=sa;pwd=sa;database=MyTestDB");
private void button1_Click(object sender, EventArgs e)
{
int rtnNum = 0;
openFileDialog1.Filter = "*.jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
string filePath = openFileDialog1.FileName;
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
byte[] imagebytes = new byte[stream.Length];
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
imagebytes = reader.ReadBytes(Convert.ToInt32(stream.Length));
if (conn.State==ConnectionState.Closed)
{
conn.Open();
}
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("insert into imagetable values(@image)",conn);
cmd.Parameters.Add("image", SqlDbType.Image);
cmd.Parameters["image"].Value = imagebytes;
rtnNum = cmd.ExecuteNonQuery();
if (rtnNum>0)
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("Error");
}
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
if (conn.State==ConnectionState.Closed)
{
conn.Open();
}
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select image from imagetable",conn);
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
imagebytes = (byte[])reader.GetValue(0);
}
reader.Close();
conn.Close();
System.IO.MemoryStream ms = new System.IO.MemoryStream(imagebytes);
Bitmap bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
}