Please anyone can help me to load image from sql server , it's already added in the database successfully but couldn't be loaded in the picture box ...
Here is the code
if (ds.Tables[0].Rows[i][6] != DBNull.Value)
{
label_question.Text = ds.Tables[0].Rows[i][0].ToString();
radioButton1.Text = ds.Tables[0].Rows[i][1].ToString();
radioButton2.Text = ds.Tables[0].Rows[i][2].ToString();
radioButton3.Text = ds.Tables[0].Rows[i][3].ToString();
radioButton4.Text = ds.Tables[0].Rows[i][4].ToString();
// Récupérer les données d'image depuis la colonne de la base de données
byte[] img = (byte[])ds.Tables[0].Rows[i][6];
if (img != null && img.Length > 0)
{
try
{
// Créer une image à partir des données d'image
using (MemoryStream ms = new MemoryStream(img))
{
Image image = Image.FromStream(ms);
pictureBox.Image = image;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show("Une erreur s'est produite lors de la manipulation de l'image : " + ex.Message, "Erreur");
}
Rajkiran SwainPosted Apr 26, 2023, 11:59 AM
The code you provided looks correct for loading an image from a SQL Server database into a PictureBox control. However, there could be several reasons why the image is not being displayed. Here are a few things you can check:
Verify that the image data is being retrieved from the database correctly. You can check the length of the byte array to make sure it has a non-zero length, and also use a debugger to step through the code and inspect the values of the
imgvariable.Make sure that the PictureBox control is visible on the form and has a size that is large enough to display the image.
Check that the image format is supported by the Image class. Some image formats may not be supported, so you may need to convert the image to a different format before displaying it.
Make sure that the application has sufficient permissions to access the database and retrieve the image data. You may need to check the connection string and ensure that the appropriate credentials are being used.
If you are still having trouble displaying the image, you may want to try debugging the code to narrow down the problem. You can also try searching online for similar issues to see if there are any solutions that might work for your situation.