CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2008
    Posts
    18

    Windows Forms - Saving Images..

    Hi.. I'm working with databases, Access databases to be exact, and I have a database in which there is an OLE Object type to store pictures. I have no problem retrieving these pictures and showing them in a picturebox, and actually I have no problem saving them to the database. The problem arises when I try to RETRIEVE the pictures that I'VE saved to the database through the application I've written. If I put an image in via access, I can retrieve no problem. If I put an image in via my windows form, and later try to retrieve it and throw it back into a picture box, I get the following error:
    System.ArgumentException: Parameter is not valid.
    at System.Drawing.Bitmap..ctor(Stream stream)
    I suppose, since the error ONLY arrives when I try to bring in one of the pictures saved through the form that it is the method in which its being saved, but I'll post the code for both saving and retrieving.

    Saving:
    Code:
    Byte[] byPicture;
    MemoryStream ms = new MemoryStream();
    picPicture.Image.Save(ms, picPicture.Image.RawFormat);
    byPicture = ms.ToArray();
    ..........
    DataRow newRow = thisConnection.adoDataSet.Tables["Categories"].NewRow();
    newRow["Picture"] = byPicture;
    Retrieving:
    Code:
    Byte[] byPicture;
    MemoryStream ms = new MemoryStream();
    byPicture = (Byte[])thisConnection.adoDataSet.Tables["Categories"].Rows[rowPos]["Picture"];
    ms.Write(byPicture, 78, byPicture.Length - 78);
    try
    {
    
    	 Bitmap bm = new Bitmap(ms);
    	 picPicture.Image = bm;
    }
    catch (ArgumentException ae)
    {
    	  string message = ae.ToString();
    	  MessageBox.Show(message);
    }

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Windows Forms - Saving Images..

    I guess it could be because you save the image as raw format, but load is as bmp. Try modify your save code in following way:
    Code:
    picPicture.Image.Save(ms,  ImageFormat.Bmp);
    I write it just form head, so bo warranty is provided
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jul 2008
    Posts
    18

    Re: Windows Forms - Saving Images..

    Doesn't work.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Windows Forms - Saving Images..

    Try

    Code:
    Byte[] byPicture = (Byte[])thisConnection.adoDataSet.Tables["Categories"].Rows[rowPos]["Picture"];
    MemoryStream ms = new MemoryStream(byPicture);
    Bitmap bitmap = new Bitmap(ms);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

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

Featured