Retrieving binary back to image and from database and save into a folder using WPF

ε祈祈猫儿з 提交于 2021-02-05 09:48:21

问题


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:


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)



来源:https://stackoverflow.com/questions/30442866/retrieving-binary-back-to-image-and-from-database-and-save-into-a-folder-using-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!