DataTable dt = new DataTable(); dt = eda.NavigateEmployee(); // Create datatable from NavigateEmployee Class tableBindings.DataSource = dt; Binding picture = new Binding("Image", tableBindings, ((DataTable)tableBindings.DataSource).Columns[15].ColumnName); picture.Format += new ConvertEventHandler(pictureFormat); EmployeePic.DataBindings.Add(picture); private void pictureFormat(object sender, ConvertEventArgs e) { byte[] b = (byte[])e.Value; // Error 'unable to cast object of type string to type byte[] MemoryStream ms = new MemoryStream(b); Bitmap bmp = new Bitmap(ms); ms.Close(); e.Value = bmp; } // Function to save picture to SQL Server public byte[] ImageToStream(string fileName) { MemoryStream stream = new MemoryStream(); try { Bitmap image = new Bitmap(fileName); image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Updating Photo", MessageBoxButtons.OK, MessageBoxIcon.Error); } return stream.ToArray(); } // Function to Display Selected Picture to PictureBox public void UploadPicture() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; if (ofd.ShowDialog() == DialogResult.OK) { strFilePath = ofd.FileName; EmployeePic.Image = new Bitmap(strFilePath); lblImageFilePath.Enabled = false; lblImageFilePath.Text = System.IO.Path.GetFileName(strFilePath).ToString(); } }
string
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)