首页 > 解决方案 > 从 sqlite 数据库下载 byte[] 并转换为照片

问题描述

我在将 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[]之前在数据库中工作正常,但是从数据库中下载之后就不行了。

标签: c#sqlite

解决方案


推荐阅读