图像未使用 C# 桌面应用程序保存 SQLite 数据库

Image not saving SQLite Database with C# Desktop Application

我正在尝试使用 C# 将图片框中的图像保存到 Sqlite 并从 SqliteDB 中检索图片框上的图像。 它在数据库中保存的内容您可以在此处的图片中看到,图像的数据库数据类型是 blob

当我试图在图片框中检索和显示图像时,它显示以下错误。

代码如下:

    using Finisar.SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLiteDb
{
    public partial class ImageForm : Form
    {
        public ImageForm()
        {
            InitializeComponent();
        }
        SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=False;Compress=True;");

        private void button2_Click(object sender, EventArgs e)
        {
            //To convert immage into bytes
            Image img = pictureBox1.Image;
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] dataByte = ms.ToArray();


           // Saving into database. . 
            sqlite_conn.Open();
            SQLiteCommand cmd_Insert = new SQLiteCommand("Insert into ForImage(ID,FileImage) values ('"+12+"','" + dataByte + "')", sqlite_conn);
            cmd_Insert.ExecuteNonQuery();
        }

        // To Show/load Image in Picturebox 1.
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = openFileDialog1.ShowDialog();
            if (DialogResult == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sqlite_conn.Open();

            SQLiteCommand cmd = new SQLiteCommand("Select FileImage from ForImage where ID=12", sqlite_conn);
            byte[] imageBytes = (byte[])cmd.ExecuteScalar();
            MemoryStream mm = new MemoryStream(imageBytes);
            Image img = Image.FromStream(mm);
            pictureBox2.Image = img;

        }
    }
}

请告诉我代码中有什么问题。

这是上述问题的解决方案和工作代码:

using Finisar.SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLiteDb
{
    public partial class Final_Form : Form
    {
        public Final_Form()
        {
            InitializeComponent();
        }
        SQLiteConnection con = new SQLiteConnection("Data Source=database.db;Version=3;New=False;Compress=True;");
        Image image;
        private void Upload_image_Click(object sender, EventArgs e)
        {
            DialogResult = openFileDialog1.ShowDialog();
            if (DialogResult == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
                image = pictureBox1.Image;
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            byte[] imagesBytes = Image2Byte(pictureBox1.Image);
            SaveImage(imagesBytes);
        }

        private void Show_Click(object sender, EventArgs e)
        {
            LoadImageFromDb();
        }
        //Retrive Image Code. . 

        private void LoadImageFromDb()
        {
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand("Select FileImage from ForImage where ID='86';", con);
            byte[] arrayFromDb = (byte[])cmd.ExecuteScalar();
            con.Close();
            pictureBox2.Image = ByteToImage(arrayFromDb);
        }
        private Image ByteToImage(byte[] toConvert)
        {
            MemoryStream toImage = new MemoryStream(toConvert);
            Image imageFromBytes = Image.FromStream(toImage);
            return imageFromBytes;
        }
        //Picture Save Methods Define Here. . 
        #region
        private void SaveImage(byte[] imageBytes2Save)
        {
            string Query_ = "INSERT INTO ForImage (ID,FileImage) VALUES (86,@0);";
            SQLiteParameter PicParam = new SQLiteParameter("@0", DbType.Binary);
            PicParam.Value = imageBytes2Save;

            con.Open();
            SQLiteCommand cmd = new SQLiteCommand(Query_,con);
            cmd.Parameters.Add(PicParam);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        private byte[] Image2Byte(Image imageToconvert)
        {
            MemoryStream memoryStream = new MemoryStream();
            imageToconvert.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] bytesOfImage = memoryStream.ToArray();
            return bytesOfImage;
        }

        #endregion




    }
}

// 这是输出

  • 上传图片到图片框

  • 将图片保存到数据库(SQLITE)

  • 从数据库中检索以在图片框中显示

我是怎么解决这个问题的?
答案:通过将参数放入查询中以将图像保存在数据库中。