在SqLite中存储的图像占用了2倍的空间

本文关键字:2倍 空间 图像 SqLite 存储 | 更新日期: 2023-09-27 18:13:09

我想在SqLite数据库中存储jpg图像,我正在使用此代码:

public byte[] ImageToByte(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();
                return imageBytes;
            }
        }
void btn_click()...
{
                photo = new Bitmap("invoker.jpg");
                pic = ImageToByte(photo, System.Drawing.Imaging.ImageFormat.Jpeg);
                SaveImage(pic);
}

:

void SaveImage(byte[] imagen)
        {
            string conStringDatosUsuarios = @" Data Source = 'bang.sqlite3 ;Version=3";
            SQLiteConnection con = new SQLiteConnection(conStringDatosUsuarios);
            SQLiteCommand cmd = con.CreateCommand();
            cmd.CommandText = String.Format("INSERT INTO tbl_pictures (record_id, pic) VALUES ('1', @0);");
            SQLiteParameter param = new SQLiteParameter("@0", System.Data.DbType.Binary);
            param.Value = imagen;
            cmd.Parameters.Add(param);
            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception exc1)
            {
                MessageBox.Show(exc1.Message);
            }
            con.Close();
        }

在SqLite中存储的图像占用了2倍的空间

我认为从jpg创建位图是导致大小增加的步骤。事实上,我很惊讶它只是增加了2倍的尺寸!

根据维基百科(http://en.wikipedia.org/wiki/JPEG#Sample_photographs),压缩比的范围从2.6倍到46x甚至更高。div;)

好吧,Simon找到了解决方案(不是答案)。但他没有公布最终答案。解决方案如下:

在。net程序中使用c#代码创建空白数据库(不要使用第三方创建数据库)

下面是他使用的代码片段:

无论如何,我仍将等待一个完整的答案:为什么使用第三方工具创建DB使sqlite以原来的两倍大小存储图像?它与。net

中创建的数据库有何不同?

也许像这样的东西应该有所帮助:序列化/反序列化图像存储在DB

我提到了这个问题:如何使用c#在数据库中保存图像