2020-10-28 sqlite c#保存读取图片,速度痛快的

这个重要是把图片转化为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();
        }
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值