簡體   English   中英

圖像無法使用C#Desktop Application保存SQLite數據庫

[英]Image not saving SQLite Database with C# Desktop Application

我正在嘗試將圖片框中的圖像保存到Sqlite並使用C#從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)

  • 從數據庫中檢索以在Picturebox中顯示

我是如何解決這個問題的?
Ans:通過在查詢中放置參數來保存數據庫中的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM