这个重要是把图片转化为byte【】,然后转码为base64的string数据类型,再保存到数据库里面,
读取是上面反过来过程的。
因为string可以储存2gb的数据的,所以不要怕太大的图片不够,
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.Windows.Forms;
namespace sqlite_image
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
data.createNewDatabase();
data.connectToDatabase();
data.createTable();
//data.fillTable(textBox1.Text);
//textBox2.Text = data.printHighscores();
}
data data = new data();
private void button1_Click(object sender, EventArgs e)
{
}
public Image GetImageByBytes(byte[] bytes)
{
Image photo = null;
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Write(bytes, 0, bytes.Length);
photo = Image.FromStream(ms, true);
}
return photo;
}
public byte[] GetByteImage(Image img)
{
byte[] bt = null;
if (!img.Equals(null))
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
return bt;
}
public byte[] stringtobyte(string str)
{
byte[] decBytes = Convert.FromBase64String(str);
return decBytes;
}
public string bytetostring(byte[] bytes)
{
string str = Convert.ToBase64String(bytes);
return str;
}
private void button2_Click(object sender, EventArgs e)
{
data.delectTable();
data.createTable();
data.fillTable(bytetostring(GetByteImage(pictureBox1.Image)));
}
private void button3_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap (openFileDialog.FileName);
}
}
private void button3_Click_1(object sender, EventArgs e)
{
pictureBox2.Image = GetImageByBytes(stringtobyte(data.printHighscores()));
}
}
}
/
using System;
using System.Data.SQLite;
using System.Text;
namespace sqlite_image
{
class data
{
//数据库连接
SQLiteConnection m_dbConnection;
//创建一个空的数据库
public void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}
//创建一个连接到指定数据库
public void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}
//在指定数据库中创建一个table
public void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//删除一些数据
public void delectTable()
{
string sql = "DROP table highscores ";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//DROP TABLE database_name.table_name;
//插入一些数据
public void fillTable(string s)
{
string sql = "insert into highscores (name, score) values ('"+s+"', 3000 )";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//使用sql查询语句,并显示结果
public string printHighscores()
{
string sql = "select * from highscores ";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
sb.Append(reader["name"] +"\r\n");
}
return sb.ToString();
}
}
}