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;
}
}
and before save this image into database , convert this picturebox image into byte[] format .
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
pictureBoxCompanyLogo.Image = GetDataToImage((byte[])byteimage);
thanks