我正在尝试将图片框中的图片保存到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;
}
}
}
请告诉我代码中的问题。
答案 0 :(得分:0)
以下是上述问题的解决方案和工作代码:
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:通过在查询中放置参数来保存数据库中的图像。