Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have spent 2 months trying to insert an image into database and retrieve it again!,i have succeeded to insert the image into database ,but when i try to retrieve it into picturebox i get this exception
btw i'm using scanner so i dun need to browse a file i just want to insert it from picturebox into database and retrieve it again into picturebox

Insert Step
//---------------
MemoryStream ms = new MemoryStream();
PBoxGuestImage.Image.Save(ms, ImageFormat.Jpeg);
Byte[] PicArray = new Byte[ms.Length];
ms.Position = 0;
ms.Read(PicArray, 0, PicArray.Length);

CMD.CommandText = "Insert Into TestTable(TestImage) Values (@ParTestImage)"
CMD.Parameters.AddWithValue("@ParTestImage", PicArray);
//---------------

this works great

Retrieve Step
C#
Image NewImage;
byte[] content = (byte[])TestReader["TestImage"];
TestPictureBox.Image = null;
using (MemoryStream stream = new MemoryStream(content, 0, content.Length))
{
   stream.Write(content, 0, content.Length);
   NewImage = Image.FromStream(stream, true); //at this line i get the exception
}
TestPictureBox.Image = NewImage;


is there anyway to solve this issue ?

Thanks.
Posted
Updated 19-Aug-11 21:21pm
v2

1 solution

According to Image.FromStream[^] you get that exception because the stream is not a valid image format.

Try to open a FileStream to an image and use Image.FromStream to see if it works, if it does then there is something wrong with the data you get from TestReader["TestImage"]
 
Share this answer
 
v2
Comments
snake1 19-Aug-11 11:25am    
so what should i do ?, do you have any sample of code which worked for u at this case?
Simon Bang Terkildsen 19-Aug-11 11:52am    
As I tried to say your data from TestReader["TestImage"] is properbly invalid/corrupt. So try to open a file you know is valid (one you can open in MS Paint for example)
using(Stream stream = File.Open(@"pathTo\anImage.jpg", FileMode.Open)
NewImage = Image.FromStream(stream, true);


If the above works, which it does, then you'll know whatever data you have
in TestReader["TestImage"] does not represent a valid image.
snake1 19-Aug-11 16:16pm    
yes i have tried it and it worked on a file , so is there something wrong at the insert step ?
Simon Bang Terkildsen 19-Aug-11 16:27pm    
´the data you have in TestReader["TestImage"] is invalid
snake1 19-Aug-11 16:56pm    
so what should i do in this case is it impossible to do that ?!,some people says it's very easy i knew it's very easy ,but why am i stuck here ?,please help me to solve this issue

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