2015-05-14 120 views
-3
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); 
    } 

} 

我已经检查过各种页面,但是这并没有解决我的问题。我只是在这部分得到一个错误:如何检索图像从数据库到c中的图片框#

"pictureBox1.Image = Image.FromStream(ms,true);" 
+0

仅供参考,这是一个仅限英语的部位,“PLZ”是不是英文单词。 –

回答

0

你列的数据类型应该是image尝试

public byte[] ConvertImageToByte(Image image, ImageFormat format) { var stream = new MemoryStream(); image.Save(stream, format); return stream.ToArray(); } public Image ConvertBytesToImage(object _image) { byte[] byteImage = (byte[])(_image); MemoryStream ms = new MemoryStream(byteImage); ms.Write(byteImage, 0, byteImage.Length); Image img = Image.FromStream(ms); return img; }

在数据库中保存的字节

OpenFileDialog file = new OpenFileDialog(); file.Title = "Please Select the Employee Photo (Dimension 128x128)"; file.Filter = "JPEG Files|*.jpg|Bitmap Files|*.bmp|Gif Files|*.gif|PNG Files|*.png"; file.DefaultExt = "jpg"; file.ShowDialog(); if (!string.IsNullOrEmpty(file.FileName)) { byte[] bytePhoto = ConvertImageToBytes(Image.FromFile(file.FileName), ImageFormat.Png); //bytePhoto is the object to save in your database }

检索字节和dis发挥图像

byte[] bytePhoto = (byte[])ds.Tables["empdetails"].Rows[0]["Photo"]; pictureBox1.Image = ConvertBytesToImage(bytePhoto);

相关问题