I have successfully converted image to binary and saved it into the database using linq to sql WPF and now i want to retrieve it back to image format and save it to a specific folder in computer.
i have read many blogs and articles which retrieves the image binary from database and then shows it into the PictureBox, what i want to do is to select the image and save it to a specific folder using linq to sql.
code which i have tried so far for uploading an image:
private void Browse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = ".jpg";
ofd.Filter = "Image File (.jpg) | *.jpg";
Nullable<bool> result = ofd.ShowDialog();
if(result == true)
{
string fileName = ofd.FileName;
_txtFileName.Text = fileName;
}
}
private void Upload_Click(object sender, RoutedEventArgs e)
{
using(ImageDataContext db=new ImageDataContext())
{
image_data img = new image_data();
img.image = ConverImageToBinary(_txtFileName.Text);
try
{
db.image_datas.InsertOnSubmit(img);
db.SubmitChanges();
MessageBox.Show("Picture Upload Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public static byte[] ConverImageToBinary(string convertedImage)
{
try
{
FileStream fs = new FileStream(convertedImage, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return image;
}
catch(Exception ex)
{
throw ex;//MessageBox.Show(ex.Message, "error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}