Saved images in SqLite take up twice as much space

I want to save jpg images in a SqLite database, and I'm using this code for now:

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); } 

UPDATE

 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(); } 
+4
source share
3 answers

Well, Simon found a solution (not an answer). But he did not publish the final answer. Here is the solution:

Create an empty database using C # code and inside your .NET program (do not use a third-party organization to create the database)

Here's a snippet of code it uses.

In any case, I will still wait for a complete answer. Why does creating a database using third-party tools make sqlite store images in the size of 2x the size of the original? And how is it different from a database created inside .NET.

+1
source

I think creating a bitmap from jpg is a step causing an increase in size. In fact, I am surprised that this is just a 2x increase in size!

According to wikipedia ( http://en.wikipedia.org/wiki/JPEG#Sample_photographs ), the compression ratio can vary from 2.6x to 46x and higher .;)

+2
source

Perhaps something like this should help: Serialize / Deserialize images for storage in the database

I referred to this question: How to save an image in a database using C #

0
source

Source: https://habr.com/ru/post/1480360/


All Articles