有人可以帮助我在将图像从数据库上传到picturebox后,如何将图像保存在picturebox中。我的问题是,一切正常,除了关闭窗口后图像消失,我需要点击按钮才能显示它,图像在上传后如何自动显示在图片框中?即使我关闭窗口,如何保持img保存到PictureBox
这是我上传的代码上点击:
private void button2_Click(object sender, EventArgs e)
{
//DB Connection string
string strConn;
strConn = "Data Source=MARINCHI\\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True";
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
//Retriver img from DB into Dataset
SqlCommand sqlCmd = new SqlCommand("SELECT id, image FROM user2 ORDER BY id", conn);
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
sqlDA.Fill(ds, "image");
int c = ds.Tables["image"].Rows.Count;
if (c > 0)
{
Byte[] bytIMGDATA = new Byte[0];
bytIMGDATA = (Byte[])(ds.Tables["image"].Rows[c - 1]["image"]);
using (MemoryStream stmIMGDATA = new MemoryStream(bytIMGDATA))
{
pictureBox1.Image = Image.FromStream(stmIMGDATA);
}
MessageBox.Show("File read from database succesfully");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我也尝试添加下面的链接(pictureBox1.Image = Image.FromStream(stmIMGDATA);
)
pictureBox1.Image.Save(stmIMGDATA, pictureBox1.Image.RawFormat);
,然后我得到一个错误:
A generic error occurred in GDI+
你能引导我如何将图像永远保存在那里?谢谢 – Rin
@Rin我已经添加了示例如何加载图像按钮单击,以便图像保持加载,直到表单不关闭 –
我认为这就是你之前已经有的东西:OI想知道如何我可以永远保持IMG后我关闭表格并打开它,以便它仍然存在。 – Rin