【发布时间】:2013-05-12 18:04:47
【问题描述】:
我想将 jpg 图像存储在 SqLite 数据库中,我目前正在使用此代码:
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();
}
【问题讨论】:
-
SaveImage是做什么的? -
@CL。它只是将图像插入数据库。添加代码。
-
如何确定数据库中图像的大小?
SELECT length(pic) FROM tbl_pictures? -
@CL。不只是从属性窗口检查大小(右键单击文件等。)使用数据库管理工具也显示相同。
-
您要添加的内容的大小是多少?例如,我使用 525897 字节的文件对其进行了测试,生成的 db 大小为 529408 字节。有开销,但没有像 2X 因子那样。或者你是在比较结果图像字节[]的大小还是输入图像文件的大小?
标签: c# database image sqlite encoding