Click here to Skip to main content
15,891,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I have saved an image in my postgres database.

when i want to retrieve the image back in the picture box i am getting this error ArgumentException was unhandled Parameter not valid. i have pasted my codings.

What I have tried:

I have tried all sorts of solutions from internet but i could not solve this.
Please tell me where i have gone wrong.
C#
public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn); 
    Image returnImage = Image.FromStream(ms);
    return returnImage;

}

byte[] photo = System.Text.Encoding.Unicode.GetBytes(dr2["customerimage"].ToString());
pictureBox2.Image = byteArrayToImage(photo);
Posted
Updated 31-Jul-16 2:08am
v2
Comments
Ehsan Sajjad 31-Jul-16 7:47am    
the error is coming on which line?

Simple: it's probably how you saved it that is causing the problem: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
 
Share this answer
 
Something like this:
C#
command = new NpgsqlCommand("select customerimage from mytable where id = 1);", conn);
Byte[] result = (Byte[])command.ExecuteScalar();

FileStream fs = new FileStream("database", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(new BufferedStream(fs));

bw.Write(result);
bw.Flush();
fs.Close();
bw.Close();
 
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