C# 在c语言中如何将图像从数据库检索到picturebox#
我已经检查了各个页面,但这并没有解决我的问题。我只在这一部分得到一个错误:C# 在c语言中如何将图像从数据库检索到picturebox#,c#,.net,C#,.net,我已经检查了各个页面,但这并没有解决我的问题。我只在这一部分得到一个错误: private void view_Load(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=SOFT;Initial Catalog=Dev01;Integrated Secu
private void view_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=SOFT;Initial Catalog=Dev01;Integrated Security=True";
con.Open();
//Retrieve BLOB from database into DataSet.
SqlDataReader myReader = null;
SqlCommand cmd = new SqlCommand("select * from empdetails", con);
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
lbl_fname.Text = myReader["firstname"].ToString();
lbl_mname.Text = myReader["middlename"].ToString();
lbl_lname.Text = myReader["lastname"].ToString();
lbl_gender.Text = myReader["gender"].ToString();
lbl_dob.Text = myReader["dob"].ToString();
lbl_qualification.Text = myReader["qualification"].ToString();
lbl_skills.Text = myReader["skills"].ToString();
lbl_userid.Text = myReader["username"].ToString();
lbl_pwd.Text = myReader["password"].ToString();
lbl_cpwd.Text = myReader["confirmpassword"].ToString();
lbl_mno.Text = myReader["mobilenumber"].ToString();
lbl_altmno.Text = myReader["alternativenumber"].ToString();
lbl_email.Text = myReader["email"].ToString();
lbl_presentadd.Text = myReader["presentaddress"].ToString();
lbl_permanentadd.Text = myReader["permanentaddress"].ToString();
}
myReader.Close();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "empdetails");
int c = ds.Tables["empdetails"].Rows.Count;
if (c > 0)
{
//BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
//SqlCommand cmd1=new SqlCommand("Select photo from empdetails");
Byte[] bytedata = new Byte[0];
bytedata = (Byte[])(ds.Tables["empdetails"].Rows[c - 1]["photo"]);
MemoryStream ms = new MemoryStream(bytedata);
pictureBox1.Image = Image.FromStream(ms,true); //HERE I AM GETTING ERROR
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
列的数据类型应为
image
try
public byte[]ConvertImageToByte(图像图像,图像格式)
{
var stream=newmemoryStream();
保存(流、格式);
返回流ToArray();
}
public Image ConvertBytesToImage(object\u Image)
{
字节[]字节图像=(字节[])(_图像);
MemoryStream ms=新的MemoryStream(byteImage);
ms.Write(byteImage,0,byteImage.Length);
Image img=Image.FromStream(ms);
返回img;
}
在数据库中保存字节
OpenFileDialog file=newopenfiledialog();
file.Title=“请选择员工照片(尺寸128x128)”;
file.Filter=“JPEG文件|*.jpg |位图文件|*.bmp | Gif文件|*.Gif | PNG文件|*.PNG”;
file.DefaultExt=“jpg”;
ShowDialog()文件;
如果(!string.IsNullOrEmpty(file.FileName))
{
byte[]bytePhoto=ConvertImageToBytes(Image.FromFile(file.FileName),ImageFormat.Png);
//bytePhoto是要保存在数据库中的对象
}
检索字节并显示为图像
byte[]bytePhoto=(byte[])ds.Tables[“empdetails”]。Rows[0][“Photo”];
pictureBox1.Image=ConvertBytesToImage(bytePhoto)代码>仅供参考,这是一个纯英语网站,“plz”不是一个英语单词。
"pictureBox1.Image = Image.FromStream(ms,true);"