使用WPF从数据库取回二进制图像并保存到文件夹中 [英] Retrieving binary back to image and from database and save into a folder using WPF

查看:34
本文介绍了使用WPF从数据库取回二进制图像并保存到文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将图像转换为二进制文件,并使用linq to sql WPF将其保存到数据库中,现在我想将其恢复为图像格式并将其保存到计算机中的特定文件夹中.

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.

我已经阅读了许多博客和文章,这些文章和文章从数据库中检索图像二进制文件,然后将其显示到PictureBox中,我要做的就是选择图像并将其使用linq to sql保存到特定的文件夹中.

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.

用于上传图片的代码:

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

推荐答案

首先,读取图像的代码很复杂,您将其作为Stream打开,并读取所有字节.有一种方法可以做到这一点,因此您可以将整个ConverImageToBinary方法替换为

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

此外,您绝不会将任何东西转换"为任何东西,图像只是磁盘上的一个字节数组,您已经读取了它,并将其保存到数据库中,如果您将其读回并保存回去(使用这次File.WriteAllBytes)就可以了,所以

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

并确保您更改扩展名以匹配您的文件类型(因此bmp表示位图,jpg表示jpeg等)

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

这篇关于使用WPF从数据库取回二进制图像并保存到文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆