Chargement Image dans pictureBox de format Varbinary(max)

danifa Messages postés 1 Date d'inscription lundi 11 septembre 2017 Statut Membre Dernière intervention 11 septembre 2017 - 11 sept. 2017 à 15:18
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 - 12 janv. 2019 à 12:51
Je fais une application web qui interagit avec la base de donnée (sql server) mais je veux afficher l'image qui se trouve a la base dans PictureBox.
Le champ qui contient l'image est de type varbinary(max).
j’utilise la langage csharp
Que faire
A voir également:

3 réponses

vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
12 sept. 2017 à 00:01
Bonjour !
Et oui ce mot est apprécié en début de discussion !

0
comment afficher une image?
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
8 nov. 2018 à 13:27
Comme l’a dit vb95 il y a plus d’un an, on apprécie la politesse ici
0
kritikas Messages postés 2 Date d'inscription samedi 12 janvier 2019 Statut Membre Dernière intervention 18 janvier 2019
Modifié le 12 janv. 2019 à 21:24
I tried this code to work picturebox image.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

//author:Morteza Naeimabadi  Location=IRAN ,Yazd
//written in 2008/03/12

namespace SampleOfMorteza
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //We are using SQL express.
            //My database name is "PictureDb".
            SqlConnection con = new SqlConnection
                               ("Server=.;database=PictureDb;integrated security=true");
            //I have used a table named "tblUsers" and fill the fields
            SqlCommand com = new SqlCommand("insert into tblUsers
                            (fldCode,fldPic) values(1,@Pic)", con);

            //In here, I have to save the picturebox image to tblUsers
            //because you were not able to send the PictureBox1.Image
            //to field "fldPic" that is of kind
            //"image" in SQL SERVER EXPRESS, then you should do something else
            // and that is converting
            //your picture box image to an array of bytes and then sending
            //that array to database.
            //Below, we have used a stream which will be used to
            //contain our picturebox image bytes.
            MemoryStream stream=new MemoryStream();
            //through the instruction below, we save the
            //image to byte in the object "stream".
            pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            //Below is the most important part, actually you are
            //transferring the bytes of the array
            //to the pic which is also of kind byte[]
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection connect = new SqlConnection
                             ("Server=.;database=PictureDb;integrated security=true");
            SqlCommand command = new SqlCommand
                                ("select fldPic from tblUsers where fldCode=1", connect);
            //for retrieving the image field in SQL SERVER EXPRESS
            //Database you should first bring
            //that image in DataList or DataTable
            //then add the content to the byte[] array.
            //That's ALL!
            SqlDataAdapter dp = new SqlDataAdapter(command);
            DataSet ds = new DataSet("MyImages");

            byte[] MyData = new byte[0];

            dp.Fill(ds, "MyImages");
            DataRow myRow;
            myRow = ds.Tables["MyImages"].Rows[0];

            MyData = (byte[])myRow["fldPic"];

            MemoryStream stream = new MemoryStream(MyData);
            //With the code below, you are in fact converting the byte array of image
            //to the real image.
            pictureBox2.Image = Image.FromStream(stream);
        }
    }
}


EDIT : Ajout du LANGAGE dans les balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
12 janv. 2019 à 12:51
BONJOUR la charte que tu as accepté d'appliquer en t'inscrivant stipule que la politesse est appréciée ici. Elle stipule aussi que CodeS SourceS est un forum francophone. Merci donc de rédiger ta message en français, tu peux aussi aller sur le forum anglophone https://ccm.net

Enfin, "j'ai essayé" n'est ni une réponse à la question initiale, ni une nouvelle question, peux tu donc préciser ton intervention?

Merci
0
Rejoignez-nous