I am creating an application which will ultimately enable a user to save an employee's image into a SQL database using Entity Framework. I'm creating an application using WPF, MVVM and a Repository; so far so good. Within my `EmployeeViewModel` I have done the following;
N.B The data type of the field in the SQL table to save an image to is set to type image.
The code snippet below are my properties which are bound to my view.
public ImageSource Image { get { return image; } set { image = value; OnPropertyChanged("Image"); } } public string Label { get { return label; } set { label = value; OnPropertyChanged("Label"); } } public string TextBox { get { return textBox; } set { textBox = value; OnPropertyChanged("TextBox"); } }
The code snippet below are my methods to upload the image using.
public string GetFileNameNoExt(string FilePathFileName) { return System.IO.Path.GetFileNameWithoutExtension(FilePathFileName); } private ICommand uploadCommand; public ICommand ShowUploadCommand { get { if (uploadCommand == null) { uploadCommand = new RelayCommand(this.UploadExecute); } return uploadCommand; } } private void UploadExecute() { System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog(); fileDialog.Title = "Select A File"; fileDialog.InitialDirectory = ""; fileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png"; fileDialog.FilterIndex = 1; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { TextBox = fileDialog.FileName; Label = GetFileNameNoExt(TextBox.Trim()); BitmapImage bmp = new BitmapImage(new Uri(TextBox.Trim())); Image = bmp; } else { Label = "You didn't select any image file."; } }
However, Within my constructor of 'EmployeeViewModel' I assign the properties of the viewmodel to those within my EF objectsbut when assigning my Image property I get the following error appear;
public EmployeeViewModel(DataObjects.Employee e) : base("") { this.EmployeeID = e.EmployeeID; //More properties etc.. this.Image = e.Image; }Error1Cannot implicitly convert type 'byte[]' to 'System.Windows.Media.ImageSource'
Therefore, my question is: How can I convert the type 'byte' to an ImageSource to enable me to save an image to a database?
Thanks in advance.