图片框中无法访问图片 [英] image is not accessible in picture box
本文介绍了图片框中无法访问图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
保存图片的代码:
private void BtnAddEmployee_Click(object sender, EventArgs e)
{
Image myImage = Image.FromFile(imgLoc);
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
}
using (SqlConnection con1 = new SqlConnection("data source=.;Initial catalog=RMSDB;user=sa;password=ibs;"))
{
con1.Open();
using (SqlCommand com1 = new SqlCommand("INSERT INTO Employees(Emp_Pic_ImageData) VALUES (@IM)", con1))
{
com1.Parameters.AddWithValue("@IM", data);
com1.ExecuteNonQuery();
}
}
}
浏览按钮代码:
Browse Button Code:
string imgLoc = "";
private void BtnBrowseimage_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "jpg All files (*.jpg)| *.jpg" + " | All Files (*.*) | *.*";
if (dlg.ShowDialog()==DialogResult.OK)
{
imgLoc = dlg.FileName.ToString();
PicboxEmployee.ImageLocation = imgLoc;
}
}
回溯图片代码:
code for retrival image:
string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
byte[] img = (byte[])(reader[0]);
if (img == null)
{
PicboxEmployee.Image = null;
}
else
{
MemoryStream mstrm = new MemoryStream(img);
PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm); //there is error of parameter is not valid.
}
}
else
{
MessageBox.Show("this not exists");
}
推荐答案
我上次回答你对这个问题的回答:参数无效错误 [ ^ ]
数据库中的数据不是图像 - 它是您在使用参数化查询更改为保存之前加载的数据。您必须删除所有这些,然后使用新代码重新加载。
I refer you to my answer to this question last time: Parameter is not valid error[^]
The data in your database is not images - it is the data you loaded before you changed to saving them using a parameterized query. You have to delete all of those, and reload using your new code.
这篇关于图片框中无法访问图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文