Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
i want to image store into database, i know its through only byte format, i have no idea about this.

i want sample code for insert and retrieve the image from sql database in windfroms, its possible ?

Thanks in advance!!

Regards
AR
Posted
Updated 26-Dec-13 20:27pm
v3

Hi
I could see an excellent example for this. This may help you.

http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html[^]


Regards
Dominic
 
Share this answer
 
Comments
An@nd Rajan10 27-Dec-13 2:11am    
what about in winforms?
Use Google or Codeproject search, you'll get .......
Upload and Download Files with SQL Server in ASP.NET[^]
 
Share this answer
 
Comments
An@nd Rajan10 27-Dec-13 2:11am    
what about in winforms?
thatraja 27-Dec-13 2:16am    
You should have mentioned about winforms in your question. I see that you have edited your question after our answers.

Well, you could use the same code for winforms but you need to do little bit changes in that code.
An@nd Rajan10 27-Dec-13 2:23am    
yes solution after edited..
thatraja 27-Dec-13 2:25am    
well, now what?

You could use the code(Byte-Image & Image-Byte) for both windows & web application. Go ahead
Hello ,

you just take one picturebox control from the toolbox . now set the picturebox image by this way.

private void btnBrowser_Click(object sender, EventArgs e)
     {
         openFileDialog1.Title = "Choose Image";
         openFileDialog1.Filter = "Images (*.JPEG;*.BMP;*.JPG;*.GIF;*.PNG;*.)|*.JPEG;*.BMP;*.JPG;*.GIF;*.PNG";
         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             Image img = new Bitmap(openFileDialog1.FileName);
             pictureBoxCompanyLogo.Image = img;// resizeImage(img);
         }
     }


and before save this image into database , convert this picturebox image into byte[] format .
C#
public static  byte[] ImageToByteArray(Image img,PictureBox pictureBoxCompanyLogo)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (pictureBoxCompanyLogo.Image != null)
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            return ms.ToArray();
        }

and call this method
byte[] byteImg=ImageToByteArray(pictureBoxCompanyLogo.Image, pictureBoxCompanyLogo);


Now save this byte[] image in your database (in the Image column of sqlserver)

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
C#
//call "GetDataToImage" this function before load the image in picturebox
 pictureBoxCompanyLogo.Image = GetDataToImage((byte[])byteimage);


thanks
 
Share this answer
 
 
Share this answer
 
v2
Comments
An@nd Rajan10 27-Dec-13 2:11am    
what about in winforms?
Gitanjali Singh 27-Dec-13 2:14am    
http://www.tutsstore.com/how-to-insert-retrieve-image-in-sql-server/
Try this link for winforms
An@nd Rajan10 27-Dec-13 2:27am    
this link works, please update your answer, then only i accepting the answer
Gitanjali Singh 27-Dec-13 2:33am    
Done
 
Share this answer
 
v2
Comments
An@nd Rajan10 27-Dec-13 2:11am    
what about in winforms?
Tom Marvolo Riddle 27-Dec-13 2:18am    
Check my updated 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