0

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);
            }
        }

1 Answer 1

2

First of your code to read the image is way to complex, you're opening it as a Stream and reading well, all bytes. There's a method that does exactly that so you can replace your whole ConverImageToBinary method with

img.image = File.ReadAllBytes(_txtFileName.Text);

Also you never "converted" anything to anything, an image is just an array of bytes on disk, you've read it, saved it to the database, if you read it back and save it back (using this time File.WriteAllBytes) it will work just fine, so

IF you want to write to the disk then just save the image back to disk as such:

File.WriteAllBytes(@"d:\myfile.bmp",img.Image.ToArray()) ;

And make sure you change the extention to match your file type (so bmp for a bitmap jpg for a jpeg etc)

Sign up to request clarification or add additional context in comments.

9 Comments

Please note that the question is about WPF. Although mentioned in the question, there is no PictureBox in WPF. You would instead load a BitmapImage from a local file URI or from a MemoryStream, and put it in an Image control. There are of course also plenty of answers on StackOverflow that show how to do that exactly.
Really depends on what he means, he could be hosting a picturebox (winform) within wpf althought i doubt it
@RonanThibaudau.... I have used "File.WriteAllBytes(@"d:\", img.Image);" but it returns exception even thou after converting it to int and binary still it throws exception "Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.IConvertible"
@Clemens....dear actually my problem was to save the image back to folder yes i know that IMAGE control is used to display image in WPF...i appreciate you help..
Also File.WriteAllBytes could never work the way you state it, you're just telling it to save to d:\ that's a folder not a file, it needs a filename!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.