in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

load image from PictureBox.Image into Byte[] blob

Last post 03-06-2008 1:03 AM by Wiaz. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 10-23-2007 9:44 PM

    • Wiaz
    • Top 10 Contributor
    • Joined on 01-11-2007
    • Poland
    • Posts 47

    load image from PictureBox.Image into Byte[] blob

    Hi everyone! can you help me with this? I can load image from database into PictureBox with MemoryStream. I just create memmorystream from byte[] then loadbitmat from stream and then put it into PictureBox.Image. All is OK. I can even :) load Image from file into PictureBox.Image! All works fine when I use databindings (wizard etc), but if i want to load image into blob (byte[]) on my own I simply cannot do this:( I dont know how to convert PictureBox.Image to byte[] and then insert this into database. I did something but when i load Image from database then i have error with unknown format:(!!! Can anyone help me save PictureBox into blob( byte[]) ??? Thanx!
    Wiaz
  • 11-21-2007 4:30 PM In reply to

    Re: load image from PictureBox.Image into Byte[] blob

    Answer

    This code works perfectly for me: 

                   using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
                    {
                        SQLiteCommand mycommand = new SQLiteCommand(myconnection);
                        
                        SQLiteParameter myparam = new SQLiteParameter("@myparam");
                        SQLiteParameter myparam2 = new SQLiteParameter("@myparam2");

                        mycommand.CommandText = "INSERT INTO table VALUES(@myparam, @myparam2)";
                        mycommand.Parameters.Add(myparam);         // <- myparam1 is int

                        mycommand.Parameters.Add(myparam2);       // <- in myparam2 I save the blob image

                        ms = new MemoryStream();
                        myparam.Value = 1;
                        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                        myparam2.Value = ms.ToArray();              // <- here I save the image
                        mycommand.ExecuteNonQuery();
                        
                        mytransaction.Commit();
               }

      hope this work for you

  • 11-21-2007 10:56 PM In reply to

    • Wiaz
    • Top 10 Contributor
    • Joined on 01-11-2007
    • Poland
    • Posts 47

    Re: load image from PictureBox.Image into Byte[] blob

    Thanx! As I remember "ms.ToArray()" may help in my source!
    Wiaz
  • 02-26-2008 1:53 AM In reply to

    • tarix
    • Not Ranked
    • Joined on 02-26-2008
    • Posts 2

    Re: load image from PictureBox.Image into Byte[] blob

    Kudos for this.  I had a similar problem and your solution worked perfectly!

     

  • 03-05-2008 11:42 PM In reply to

    • Wiaz
    • Top 10 Contributor
    • Joined on 01-11-2007
    • Poland
    • Posts 47

    Re: load image from PictureBox.Image into Byte[] blob

    Hi, i still have a problem i cannot handle:

     

    con.Open();

    SQLiteCommand zapytanie = new SQLiteCommand("UPDATE links SET favicon=@favicon WHERE id=" + linkId.ToString(), con);

    SQLiteParameter parametr = new SQLiteParameter("@favicon");

    MemoryStream mystream = new MemoryStream();

    img.Save(mystream, img.RawFormat);//here is exception:Value cannot be null.Parameter name: encoder

    parametr.Value = mystream.ToArray();

    zapytanie.Parameters.Add(parametr);

    zapytanie.ExecuteNonQuery();

    con.Close();

     

    and img is from:

    request = (HttpWebRequest)WebRequest.Create(iconPath);
    response = (HttpWebResponse)request.GetResponse();

    receiveStream = response.GetResponseStream();

    img =
    Image.FromStream(receiveStream);

    response.Close();

     

    Please help me, why exception is raised?

     

    Wiaz
  • 03-06-2008 1:03 AM In reply to

    • Wiaz
    • Top 10 Contributor
    • Joined on 01-11-2007
    • Poland
    • Posts 47

    Re: load image from PictureBox.Image into Byte[] blob

    Wiaz:
    img.Save(mystream, img.RawFormat);//here is exception:Value cannot be null.Parameter name: encoder

    Icon.FromHandle((((Bitmap)img).GetHicon())).Save(mystream);

    :) it works, strange btu works finally!

    Wiaz
Page 1 of 1 (6 items)
Powered by Community Server (Commercial Edition), by Telligent Systems