2017-08-01 54 views
0

當我把這種方法它顯示錯誤:無效的參數,請幫助我得到錯誤:無效的參數時,我從OLEDB數據庫retrive圖像

public void DisplayDoc() 
    { 
     try 
     { 
      con = new OleDbConnection(constr); 
      con.Open(); 

      OleDbCommand cmd = new OleDbCommand(); 
      cmd = new OleDbCommand("Select pic from doc where adharno= '" + this.aadhar + "'", con); 

      OleDbDataReader dr = cmd.ExecuteReader(); 

      DataTable dt = new DataTable(); 
      dt.Load(dr); 

      DataRow row = dt.Rows[0]; 

      pictureEmployeePhoto.Image = LoadPhoto((byte[])row["pic"]); 

      con.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error :" + ex.Message); 
     } 
    } 

    private Image LoadPhoto(byte[] photo) 
    { 
     MemoryStream ms = new MemoryStream(photo); 
     return Image.FromStream(ms); //Getting Error Here: Invalid Parameter 
    } 

here is my Doc Table in which i have stored image as shown

下面的方法我都用過以將圖像存儲在數據庫中

byte[] Pic; 
    OpenFileDialog ofdPhoto; 

    MemoryStream ms = new MemoryStream(); 

    private void btnPhotoUpload_Click(object sender, EventArgs e) 
    { 
     // open file dialog 
     ofdPhoto = new OpenFileDialog(); 
     // image filters 
     ofdPhoto.Title = "Select Employee Photo"; 
     ofdPhoto.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
     ofdPhoto.FileName = null; 

     if (ofdPhoto.ShowDialog() == DialogResult.OK) 
     { 
      // display image in picture box 
      pictureEmployeePhoto.Image = new Bitmap(ofdPhoto.FileName); 
      // image file path 
      txtPhotoFilePath.Text = ofdPhoto.FileName; 

      try 
      { 
       // Here get_image is a function and Big is the byte[] type 
       Pic = get_image(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error" + ex.Message.ToString()); 
      } 
     } 
    } 

    public byte[] get_image() 
    { 
     MemoryStream ms = new MemoryStream(); 
    pictureEmployeePhoto.Image.Save(ms,pictureEmployeePhoto.Image.RawFormat); 
     return ms.GetBuffer(); 
    } 
+0

哪一行代碼給你錯誤? –

+0

return Image.FromStream(ms); //錯誤在這裏:無效參數 – ehh

+0

是Chetan Ranpariya –

回答

0

嘗試此代碼。

public Image byteArrayToImage(byte[] byteArrayIn) 
{ 
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); 
    Image img = (Image)converter.ConvertFrom(byteArrayIn); 
    return img; 
} 
+0

Image img =(Image)converter.ConvertFrom(img_arr1); //顯示錯誤:無效參數 –

相關問題