Click here to Skip to main content
15,902,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am doing windows application by using vb,c#.
i inserted images as a image type in database through sql query. now i want to display in picturebox..
is this possible..
try to send coding or
give reference sites...

thanks,
kalai....
Posted
Updated 25-Nov-11 23:42pm
v3

you use nchar or varchar datatype for image inside the database.
 
Share this answer
 
Comments
kalaiking 26-Nov-11 5:52am    
oh.. k.. thanks...i forget..
 
Share this answer
 
Comments
kalaiking 26-Nov-11 5:52am    
thanks mr.
kalaiking 26-Nov-11 7:02am    
Error:
Parameter is not valid.
(Image returnImage = Image.FromStream(ms);)

to OriginalGriff..
RaviRanjanKr 27-Nov-11 14:33pm    
My 5+
Do not use an image field to your database at will occupy big space from your DB server. Change it to varchar instead and save the file name. With that, you can just retrieve the filename from the database and use it as source of image in your picture box.

Mark this as answer if this fixed your problem

Best regards,
Eduard
 
Share this answer
 
Comments
kalaiking 26-Nov-11 5:49am    
i did both..
my question is 'is this possible?'
OriginalGriff 26-Nov-11 6:50am    
Not always a good solution - it depends on whether the server gives access to it's file store (which would not be happening with my SQL servers). The advantage of storing the images in the database is that they are all kept together, and the db cannot get out-of-sync with the file system - particularly important if they are on different hardware.
 
Share this answer
 
1) Retrieve the image data from the database as an array of bytes.
2) Convert the bytes to an Image:
MemoryStream ms = new MemoryStream(bytes);
Image returnImage = Image.FromStream(ms);

3) Set the Image property of the Picture box to the converted data.
myPictureBox.Image = returnImage;




i tried .. but i got error.. guide me.. below my code

C#
private void Form1_Load(object sender, EventArgs e)
        {
            cn.Open();
            cmd=new SqlCommand("select * from img where id=6",cn);
            cmd.Connection=cn;
            dr=cmd.ExecuteReader();
            byte bytes;
            
            while (dr.Read())
            {
                double dble = Convert.ToDouble(dr[2].ToString());
                bytes = Convert.ToByte(dble);
                MemoryStream ms = new MemoryStream(bytes);
                Image returnImage = Image.FromStream(ms);
                pictureBox1.Image = returnImage;
            
                
            }
            cn.Close();
        }
Don't retrieve the data as a double - a double is a floating point number occupying 8 bytes. And image (of any type) is going to be bigger than that!
You need to retrieve an array of bytes, not a single byte, so declare it as such.
In addition, don't use a loop there - since you are only using a single row, you only need an if statement.
BTW: Don't retrieve info from your database that you aren't going to use. In this case, it doesn't make a significant difference, but since you are storing images in the table, if you are not going to use them, don't retrieve them - it wastes bandwidth & memory and slows the program down, possibly significantly depending on where your SQL server is. And never use magic numbers with databases! If someone else comes along and re-orders the columns in your database, you code stops working and you don't know why! :laugh: Use names field access instead.
And remember - you are responsible for cleaning up after yourself! Dispose scarce connections or use a using block:
So:
C#
private void Form1_Load(object sender, EventArgs e)
    {
        cn.Open();
        using (cmd=new SqlCommand("SELECT myImage FROM img WHERE id=6",cn))
        {
            cmd.Connection=cn;
            dr=cmd.ExecuteReader();
            if (dr.Read())
            {
                byte[] bytes = (byte[]) dr["myImage"];
                MemoryStream ms = new MemoryStream(bytes);
                Image returnImage = Image.FromStream(ms);
                pictureBox1.Image = returnImage;
            }
        }
        cn.Close();
    }
 
Share this answer
 
v2
Comments
kalaiking 26-Nov-11 5:53am    
thanks.. i goto try..
kalaiking 26-Nov-11 6:20am    
i tried .. but i got error.. guide me.. below my code

private void Form1_Load(object sender, EventArgs e)
{
cn.Open();
cmd=new SqlCommand("select * from img where id=6",cn);
cmd.Connection=cn;
dr=cmd.ExecuteReader();
byte bytes;

while (dr.Read())
{
double dble = Convert.ToDouble(dr[2].ToString());
bytes = Convert.ToByte(dble);
MemoryStream ms = new MemoryStream(bytes);
Image returnImage = Image.FromStream(ms);
pictureBox1.Image = returnImage;


}
cn.Close();
}
OriginalGriff 26-Nov-11 6:47am    
Answer updated
kalaiking 26-Nov-11 7:01am    
Parameter is not valid.
kalaiking 26-Nov-11 7:14am    
Parameter is not valid.
(Image returnImage = Image.FromStream(ms);)

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