Click here to Skip to main content
16,023,047 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
This is my code, but i have an error that , the parameter(MS) is not valid. and i used the function at the last , but again i had that error that parameter is not valid!
C#
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + bankaddress);
            OleDbCommand com = new OleDbCommand();
            con.Open();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            com.CommandText = "SELECT [Attachment],[FileName] From Dan_Attach WHERE ID=94";
            OleDbDataReader Reader2 = com.ExecuteReader();
            while (Reader2.Read())
            {
                MessageBox.Show(Reader2.GetValue(1).ToString());
                byte[] pic = new byte[2048];
                Reader2.GetBytes(0,0,pic,0,2048);
                MemoryStream MS = new MemoryStream(pic);
                Image returnImage = Image.FromStream(MS);
                pictureBox1.Image = returnImage;
                con.Close(); 
            }
          public Image byteArrayToImage(byte[] byteArrayIn)
         {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
         }
Posted
Updated 17-Apr-13 4:43am
v3
Comments
Prasad Khandekar 15-Apr-13 10:22am    
I think you are not reading the entire image data. If you at the documentation you will notice that you can obtain the actual field length (number of bytes) by passing buffer as null. You can then use this length to construct the buffer and read all image data.
Sergey Alexandrovich Kryukov 15-Apr-13 10:45am    
Basically, that would be a right solution. I put a mode detailed answer, please see.
Your comment is of course credited.
—SA

Have a look at how I do it: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - you can't just load a small protion of the data and assume that is a valid image - it isn't.
 
Share this answer
 
Please see the comment to the question by Prasad Khandekar. You can see the at the example of this technique in the MSDN help page on System.Data.DataTableReader.GetBytes:
http://msdn.microsoft.com/en-us/library/system.data.datatablereader.getbytes.aspx[^].

Your went in a wrong directions when you started to hard-code all of your immediate constants like table index, connection string and query, but hard-code 2048 cannot be explained by any logic.

This is not the only way to store and retrieve images. Please check: http://www.codeproject.com/search.aspx?q=%28database+OR+SQL%29+%28image+OR+images%29[^] :-).

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900