1

How to save image captured using webcam in Wpf application to sqlite database

Here is my code to save .

public static void SaveImageCapture(BitmapSource bitmap)
{
    try
    {

    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));
    encoder.QualityLevel = 100;


    MemoryStream mm = new MemoryStream();
    encoder.Save(mm);
    byte[] img = mm.GetBuffer();
        DataTable dt = new DataTable();
        SQLiteConnection con = new SQLiteConnection(@"data source=F:\Mayur\Vistrack\SqliteDatabases\Vistrack");
        con.Open();
        SQLiteCommand cmd = new SQLiteCommand("insert into img values('" + img + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {

    }

}

and for Loading image:

BitmapImage bmp = new BitmapImage();
DataTable dt = new DataTable();
SQLiteConnection con = new SQLiteConnection(@"data source=F:\Mayur\Vistrack\SqliteDatabases\Vistrack");
con.Open();
SQLiteCommand cmd = new SQLiteCommand("select * from img", con);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
//cmd.ExecuteNonQuery();
con.Close();
byte[] b = (byte[])dt.Rows[0][0];
MemoryStream byteStream = new MemoryStream(b);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = byteStream;
image.EndInit();
imgCapture.Source = image;

while assignning image to my image controle imgCapture it's showing :No imaging component suitable to complete this operation was found.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Mayur Pawar
  • 107
  • 2
  • 16
  • I have bitmapSource image from webcam how can i save it in database means in which format i have to convert it first and how. – Mayur Pawar Sep 24 '14 at 06:54
  • Take a look at one of the classes derived from BitmapEncoder. – Clemens Sep 24 '14 at 07:16
  • @Clemens: thank you sir.I have save the image to database and retrieve it back while assigning to image control it's showing following error :No imaging component suitable to complete this operation was found. – Mayur Pawar Sep 25 '14 at 14:49
  • Then you have somehow corrupted the image buffer. You usually get this error message when WPF tried to decode an image from an invalid buffer. Did you check the byte array returned from dt.Rows[0][0]? – Clemens Sep 25 '14 at 15:50
  • @Clemens Sir i have stored image in sqlite database in blob format.while debugging value of dt.Rows[0][0] showing System.Byte[] and byte array named b becomes byte array of size 13 and it does contain data.Tried to attached snapshot but have no credit for that. – Mayur Pawar Sep 26 '14 at 05:59
  • 13 bytes doesn't seem to be very much. What size is your bitmap? Anyway, where you save the image you should replace `byte[] img = mm.GetBuffer();` by `byte[] img = mm.ToArray();`. And I doubt that your SQLiteCommand is ok like that. I'd expect that you would have to add the blob as a command parameter. But I'm not an SQLite expert... – Clemens Sep 26 '14 at 06:53
  • @Clemens Using mm.ToArray have same results.While saving byte array its size is 60213 but at the time of retrieval it comes 13 bytes. – Mayur Pawar Sep 26 '14 at 07:42
  • As said, your saving SQLiteCommand looks dubious. See [here](http://msdn.microsoft.com/en-us/library/4f5s1we0.aspx). – Clemens Sep 26 '14 at 08:58
  • And [here](http://stackoverflow.com/a/625485/1136211). – Clemens Sep 26 '14 at 09:09
  • @Clemens:thank you very much sir adding image to database as command parameter works. – Mayur Pawar Sep 26 '14 at 09:18

0 Answers0