从sqlite数据库下载byte []并转换为照片

时间:2018-05-29 13:19:18

标签: c# sqlite

将byte []从SQLITE数据库转换为照片时遇到问题。

public List<Tuple<string, Tuple<byte[], byte[]>>> GetNameAndImagesPositions()
        {
            sqlite_conn = new SQLiteConnection(conn);
            sqlite_conn.Open();

            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "SELECT NAME, IMG_BALCONY, IMG_CROSS FROM MAIN";
            sqlite_datareader = sqlite_cmd.ExecuteReader();

            List<Tuple<string, Tuple<byte[], byte[]>>> res = new List<Tuple<string, Tuple<byte[], byte[]>>>();

            while (sqlite_datareader.Read())
            {
                Tuple<string, Tuple<byte[], byte[]>> tup = new Tuple<string, Tuple<byte[], byte[]>>(sqlite_datareader.GetString(0),
                            new Tuple<byte[], byte[]>(
                                (byte[])sqlite_datareader["IMG_BALCONY"],
                                (byte[])sqlite_datareader["IMG_CROSS"]));
                res.Add(tup);
            }

            sqlite_conn.Close();

            return res;
        }

在数据库中,照片(byte [])存储在BLOB中。

DatabasePositions dbp = new DatabasePositions();
            List<Tuple<string, Tuple<byte[], byte[]>>> list = new List<Tuple<string, Tuple<byte[], byte[]>>>();
            list = dbp.GetNameAndImagesPositions();

            for (int i = 0; i < list.Count; i++)
            {
                foreach (var elm in list)
                {
                    string nameFile1 = elm.Item1 + "(1)";
                    string nameFile2 = elm.Item1 + "(2)";

                    Uri pat = new Uri(Directory.GetCurrentDirectory() + "\\report\\img\\" + nameFile1 + ".png");
                    using (FileStream outStream = new FileStream(pat.LocalPath, FileMode.Create))
                    {
                        var ms = new MemoryStream(elm.Item2.Item1);
                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(ms));
                        encoder.Save(outStream);
                    }
                }
            }

错误:在行encoder.Frames.Add(BitmapFrame.Create(ms));

System.NotSupportedException: "The appropriate image processing component needed to perform this operation was not found

我怀疑从数据库转换byte []有问题。这样做的原因是在数据库中工作前的byte []工作正常,但不再从数据库下载后。

0 个答案:

没有答案