Results 1 to 5 of 5

Thread: Saving Images in Databases

Threaded View

  1. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Saving Images in Databases

    Quick question:

    What do I do if the user decided not to choose an image?

    I have an Access 2007 database that can store an image using your code, the image is optional. If the user decides not to choose an image then I don't know what to store in the database.

    In the GUI I have a Picturebox that shows the image the user has chosen. They use an OpenFileDialog to choose an image location, and I then set the ImageLocation property of the PictureBox to that path.
    Then, when saving, I take the Image property of the PictureBox directly as the Image object you use in your code.

    But when the user did not chose an image, the Image is null.


    I am using this code to load:
    csharp Code:
    1. private Image LoadImage(byte[] picData)
    2.         {
    3.             Image pic = null;
    4.             using (MemoryStream stream = new MemoryStream(picData))
    5.             {
    6.                 pic = Image.FromStream(stream);
    7.             }
    8.             return pic;
    9.         }

    And I am using this code to save:
    csharp Code:
    1. public void ExecuteNonQuery(string sql, Image pic)
    2.         {
    3.             if (this.IsOpen())
    4.             {
    5.                 using (OleDbCommand cmd = new OleDbCommand())
    6.                 {
    7.                     cmd.Connection = _connection;
    8.                     cmd.CommandType = System.Data.CommandType.Text;
    9.                     cmd.CommandText = sql;
    10.  
    11.                     using (MemoryStream stream = new MemoryStream())
    12.                     {
    13.                         cmd.Parameters.Clear();
    14.                         if (pic != null)
    15.                         {
    16.                             pic.Save(stream, ImageFormat.Jpeg);
    17.                             cmd.Parameters.Add("@foto", OleDbType.Binary).Value = stream.GetBuffer();
    18.                         }
    19.                         else
    20.                         {
    21.                             // what to do?
    22.                             cmd.Parameters.Add("@foto", OleDbType.Binary).Value = new byte[] {};
    23.                         }
    24.                     }
    25.                     cmd.ExecuteNonQuery();
    26.                 }
    27.             }
    28.             else
    29.             {
    30.                 throw new Exception(strNO_DB_CONNECTION);
    31.             }
    32.         }
    (The @foto parameter will always be present in the sql that I pass to this function.)

    I tried various things for the value of the @foto parameter:
    - null
    - 0
    - new byte[] {}
    - simply stream.GetBuffer() as if there was a picture.

    None of them work. I keep getting an exception "Parameter is not valid." on the Image.FromStream method in the Load function...

    I can't simply not save it when the user didn't chose an image, because it is possible for the user to edit a record and remove the image that was previously chosen. So if I don't do anything that change wouldn't be recorded as the old image would still be in the database.


    To fix this I can check if the picData array has zero length. That works when I simply store stream.GetBuffer on a new (empty) stream, but I'm not really sure if that's the 'accepted' way of doing it. What am I storing in the database in that case?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width