0

I have certain images in my sqlite3 database and I was trying to retrieve it into my windows application using .Net framework 4.5. For the following code, I am getting an error stating

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll Additional information: Parameter is not valid.

This always seems to occur due to the memory stream.

private void button1_Click(object sender, EventArgs e)
{
    using( MemoryStream ms = new MemoryStream(GetImageFromDB(1), false))
    {
        Image dbImg = Image.FromStream(ms);     //System.ArgumentException--Parameter Not Valid

        dbImg.Save("Testing", System.Drawing.Imaging.ImageFormat.Png);
    }
}
private byte[] GetImageFromDB(int ImgId)
{
    byte[] btImage;
    sql_con = new SQLiteConnection("Data Source=test.db; Version=3;New=False;Compress=True;");
    string str = "select org_file_header_blob from file where _id = 1;";
    sql_cmd = new SQLiteCommand(str, sql_con);
    sql_con.Open();
    btImage = (byte[])sql_cmd.ExecuteScalar();
    sql_con.Close();
    return btImage;
}

I have used a similar code before, and it has worked well. I have tried several methods to get rid of the error, but every time its the same. I have tried:

Getting it into a Bitmap object instead of an Image object using:

Bitmap dbImg = new Bitmap(ms);

and

...
sql_con.Close();
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(btImage);
bitmap1.Save("Testing", System.Drawing.Imaging.ImageFormat.Png);
...

I have even checked the length of btImg after the sql_con.Close() using the btImg.length command. It gives me 10 as the output. Which, if I assume is 10 bytes, would not make any sense since it is an image. When I execute the same query in the sqlite3 database, I getenter image description here

This is basically 2 blank lines which is where I assume is the image.

Any ideas on what I could possibly be missing?

Edit:
P.S. Just to clarify, I am trying to convert an existing byte[] into an image

Sanketh. K. Jain
  • 489
  • 1
  • 9
  • 24
  • `I have used a similar code before, and it has worked well.` what part actually worked well in the past can you be more specific..? did anything change for example the datatype of the image field in the DB...? take a look at this for example http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array – MethodMan Oct 13 '14 at 19:13
  • The link that you've asked me to check is to convert images into a byte array. But I need to do the vice-versa. I have my byte array from the database. Also, that was a different project as a whole, using SQL server. But I don't believe that has anything to do with this. – Sanketh. K. Jain Oct 14 '14 at 02:00

2 Answers2

0

Try checking the way you write/read the image data from the database, look here How do i store and retrieve a blob from sqlite

Community
  • 1
  • 1
Amorphis
  • 398
  • 2
  • 19
  • I have tried many ways of getting the image from the database. It doesn't matter. All the ways to read/write the database ultimately do the exact same task. – Sanketh. K. Jain Oct 14 '14 at 18:07
0

Right, I finally figured it out.

There was nothing wrong with the code. The data that is being read from the database is showing as 10, because it is able to read only 10 bytes of data, which is just some part of the whole image. I assume that the rest of the image got lost in some sort of transaction.
Anyways, thanks to those who tried helping.
In the end, it really wasn't a coding error, because whoever inserted the values before me did some twisted job. Hehe.
Sanketh. K. Jain
  • 489
  • 1
  • 9
  • 24