There is a sqlite database, it has a photo column. I need to take a picture from the base and save it to my computer (to the folder). There is the following code:

private void getImage(string file) { string query = "SELECT photo FROM Tasks WHERE id=" + list_comments[i]; SQLiteCommand cmd = new SQLiteCommand(query, db); try { IDataReader rdr = cmd.ExecuteReader(); try { while (rdr.Read()) { byte[] a = (System.Byte[])rdr[0]; ByteToImage(a, file); } } catch (Exception exc) { /*MessageBox.Show(exc.Message);*/ } } catch (Exception ex) { /*MessageBox.Show(ex.Message);*/ } } public void ByteToImage(byte[] imageBytes, string fileName) { // Convert byte[] to Image MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = new Bitmap(ms); //pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); } 

But it does not work. How to implement correctly?

  • How exactly does not work? Gives an error message? Anything nk saves? - PashaPash
  • one
    And how did the picture get into the database? In what format was it originally? - Alexander Petrov
  • If your image is stored in the database in a binary form, then why do you need to convert the binary form to a Bitmap? Save the byte array to the file with the required extension and that's it. - Rootware
  • it is not very convenient to store in bytes it is better to store the picture on the server in a folder and in the database its address and simply download by address - Sasuke

0