Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written this code for retrieving image from database to picturebox. I got these error 'Parameter is not valid' at this line 'pictureBox1.Image = Image.FromStream(stm);

C#
Byte[] imagebyte = new Byte[0];
                   imagebyte = (Byte[])(dr["Pic_Image"]);

MemoryStream stm = new MemoryStream(imagebyte);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromStream(stm);

It is storing that image as 0x53797374656D2E44726177696E672E4269746D6170 in database
would u pls solve this error.
Posted
Updated 9-Oct-14 23:38pm
v2

So your steps would be like this

1.This code retrieves the rows from the <sometable> table in the database into a DataSet, copies the most recently added image into a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Image property of the PictureBox control.

2. After getting data into Dataset, count the no. of images retrieved into datatable.


C#
if(c>0)
    {   //BLOB is read into Byte array, then used to construct MemoryStream,
        //then passed to PictureBox.
        Byte[] byteBLOBData =  new Byte[0];
        byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
        MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
        pictureBox1.Image= Image.FromStream(stmBLOBData);
    }


"C" = count of rows retrieved from data table
 
Share this answer
 
Comments
TarunKumarSusarapu 9-Oct-14 3:03am    
Thanks for your Information but still i am getting same error 'parameter is not valid'
at pictureBox1.Image= Image.FromStream(stmBLOBData);
TarunKumarSusarapu 9-Oct-14 3:06am    
I have taken field datatype as image
Rohit R Chavan 9-Oct-14 4:19am    
Refer this it might give you complete idea of implementation.
https://www.daniweb.com/software-development/csharp/code/217419/how-to-insertretrieve-image-in-sql-server-database
Hello ,

While retrieving the image from Sqlserver convert the byte[] into original format by this way.

public Image GetDataToImage(byte[] pData)
        {
            try
            {
                ImageConverter imgConverter = new ImageConverter();
                return imgConverter.ConvertFrom(pData) as Image;
            }
            catch (Exception ex)
            {
                MsgBox.Show(ex.Message, "Error", MsgBox.Buttons.OK, MsgBox.Icon.Error);
                return null;
            }
        }

and ultimately set the picturebox image by call "GetDataToImage" this function before load the image in picturebox
pictureBox1.Image = GetDataToImage((byte[])byteimage);

thanks
 
Share this answer
 
Comments
TarunKumarSusarapu 9-Oct-14 2:54am    
Thanks for your information but it is also getting same error'Parameter is not valid'
Animesh Datta 9-Oct-14 3:16am    
Where have you saved the image ? in directory or in db ?
TarunKumarSusarapu 9-Oct-14 3:20am    
in Db
Animesh Datta 9-Oct-14 3:25am    
I would suggest you to follow below link to save and retrieve image from database

http://www.codeproject.com/Tips/465950/Why-do-I-get-a-Parameter-is-not-valid-exception-wh
TarunKumarSusarapu 10-Oct-14 5:06am    
In database it stored like this 0x53797374656D2E44726177696E672E4269746D6170

Can u pls solve the error
In database it stored like this 0x53797374656D2E44726177696E672E4269746D6170

Can u pls solve the error
 
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