none
Erreur GDI RRS feed

  • Question

  • Salut tout le monde

    Je charge une photo de cette facon

     ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
                ofd.Filter = "JPEGs|*.jpg";
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    img1.Image = null;
                    img1.Image = Image.FromFile(ofd.FileName);
                    C.FileName = ofd.FileName;
                }

    et j'utilise ceci pour la sauvegarder et relire de la base de données

      // convert image to byte array
        static public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray(); 
        }
    
        //Byte array to photo
        static public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }


    puis je la charge de cette facon a partir de la base de données

    using (SqlCeConnection cn = new SqlCeConnection(C.ConnectString()))
                {
                    using (SqlCeCommand cmd = new SqlCeCommand("SELECT Photo FROM Data WHERE Id=" + lblID.Text, cn))
                    {
                        if (cn.State == ConnectionState.Closed) { cn.Open(); }
                        byte[] imageData = (byte[])cmd.ExecuteScalar();
                        if ((imageData != null))
                        {
                            using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                            { 
                                ms.Write(imageData, 0, imageData.Length);  
                                img1.Image = Image.FromStream(ms, false);
                            }
                        }
                    } cn.Close();
                }


    losrque je tente de changer la photo et de passer par la commande update comme suit

    C.photo = C.imageToByteArray(img1.Image); 
    C.UpdateData(lblID.Text);

    ma class la commande update est la suivante

     static public void UpdateData(string ID)
        {
            SqlCeConnection cn = new SqlCeConnection(ConnectString());
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            sql = "UPDATE Data SET Adress='" + adress + "', City='" + city + "', PCode='" + pcode + "', State='" + state + "', Country='" + country + "', Tel='" + tel + "', Cell='" + cell + "', EMail='" + email + "', Photo='" + photo + "' WHERE Id= '" + ID + "'";
            SqlCeCommand cmd = new SqlCeCommand(sql, cn);
            cmd.Parameters.Add(new SqlCeParameter("@Adress", SqlDbType.NVarChar, 256));
            cmd.Parameters.Add(new SqlCeParameter("@City", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlCeParameter("@PCode", SqlDbType.NVarChar, 15));
            cmd.Parameters.Add(new SqlCeParameter("@State", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlCeParameter("@Country", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlCeParameter("@Tel", SqlDbType.NVarChar, 25));
            cmd.Parameters.Add(new SqlCeParameter("@Cell", SqlDbType.NVarChar, 25));
            cmd.Parameters.Add(new SqlCeParameter("@EMail", SqlDbType.NVarChar, 256));
            cmd.Parameters.Add(new SqlCeParameter("@Photo", SqlDbType.Image));
            cmd.Parameters["@Adress"].Value = adress;
            cmd.Parameters["@City"].Value = city;
            cmd.Parameters["@PCode"].Value = pcode;
            cmd.Parameters["@State"].Value = state;
            cmd.Parameters["@Country"].Value = country;
            cmd.Parameters["@Tel"].Value = tel;
            cmd.Parameters["@Cell"].Value = cell;
            cmd.Parameters["@EMail"].Value = email;
            cmd.Parameters["@Photo"].Value = photo;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

    j'obtient l'erreur suivante

    Une exception non gérée du type 'System.Runtime.InteropServices.ExternalException' s'est produite dans System.Drawing.dll

    Informations supplémentaires : Une erreur générique s'est produite dans GDI+.

    ou est l'erreur dans mon code

    Merci


    Daniel

    dimanche 18 janvier 2015 18:20

Réponses

  • Re bonjour

    Finalement j'ai trouver mes erreur je dois le sauvegarder de cette

                //Image to byte[]
                Image myImage = Image.FromFile(ofd.FileName);
                System.IO.MemoryStream imgMemoryStream = new System.IO.MemoryStream();
                myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imgByteData = imgMemoryStream.GetBuffer();  
                SqlCeConnection cn = new SqlCeConnection(C.ConnectString());
                if (cn.State == ConnectionState.Closed)
                {
                    cn.Open();
                }
                SqlCeCommand cmd;
                String sql = "insert into Data "
                    + "(LName, FName, Adress, City, PCode, State, Country, Tel, Cell, EMail, Photo) "
                    + "values (@Lastname, @Firstname, @Adress, @City, @PCode, @State, @Country, @Tel, @Cell, @EMail, @Photo)";
                cmd = new SqlCeCommand(sql, cn);
                cmd.Parameters.AddWithValue("@Lastname", t1.Text);
                cmd.Parameters.AddWithValue("@Firstname", t2.Text);
                cmd.Parameters.AddWithValue("@Adress", t3.Text);
                cmd.Parameters.AddWithValue("@City", t4.Text);
                cmd.Parameters.AddWithValue("@PCode", t5.Text);
                cmd.Parameters.AddWithValue("@State", t6.Text);
                cmd.Parameters.AddWithValue("@Country", t7.Text);
                cmd.Parameters.AddWithValue("@Tel", t8.Text);
                cmd.Parameters.AddWithValue("@Cell", t9.Text);
                cmd.Parameters.AddWithValue("@EMail", t10.Text);
                cmd.Parameters.AddWithValue("@Photo", imgByteData);
                cmd.ExecuteNonQuery();
                cn.Close();
                cn = null; 


    Puis je fait mon update de cette facon et pour eviter les erreur de chemin vide

    //Image to byte[]            
                if(ofd.FileName != "")
                {
                Image myImage = Image.FromFile(ofd.FileName);
                System.IO.MemoryStream imgMemoryStream = new System.IO.MemoryStream();
                myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imgByteData = imgMemoryStream.GetBuffer(); 
                SqlCeConnection cn = new SqlCeConnection(C.ConnectString());
                if (cn.State == ConnectionState.Closed)
                {
                    cn.Open();
                }
                String sql = "UPDATE Data SET Adress=@Adress, City=@City, PCode=@PCode, State=@State, Country=@Country, Tel=@Tel, Cell=@Cell, EMail=@EMail, Photo=@Photo WHERE Id= '" + lblID.Text + "'";
                SqlCeCommand cmd = new SqlCeCommand(sql, cn);             
                cmd.Parameters.Add(new SqlCeParameter("@Adress", SqlDbType.NVarChar, 256));
                cmd.Parameters.Add(new SqlCeParameter("@City", SqlDbType.NVarChar, 50));
                cmd.Parameters.Add(new SqlCeParameter("@PCode", SqlDbType.NVarChar, 15));
                cmd.Parameters.Add(new SqlCeParameter("@State", SqlDbType.NVarChar, 50));
                cmd.Parameters.Add(new SqlCeParameter("@Country", SqlDbType.NVarChar, 50));
                cmd.Parameters.Add(new SqlCeParameter("@Tel", SqlDbType.NVarChar, 25));
                cmd.Parameters.Add(new SqlCeParameter("@Cell", SqlDbType.NVarChar, 25));
                cmd.Parameters.Add(new SqlCeParameter("@EMail", SqlDbType.NVarChar, 256));
                cmd.Parameters.Add(new SqlCeParameter("@Photo", SqlDbType.Image));
                cmd.Parameters["@Adress"].Value = t3.Text;
                cmd.Parameters["@City"].Value = t4.Text;
                cmd.Parameters["@PCode"].Value = t5.Text;
                cmd.Parameters["@State"].Value = t6.Text;
                cmd.Parameters["@Country"].Value = t7.Text;
                cmd.Parameters["@Tel"].Value = t8.Text;
                cmd.Parameters["@Cell"].Value = t9.Text;
                cmd.Parameters["@EMail"].Value = t10.Text; 
                cmd.Parameters["@Photo"].Value = imgByteData; 
                cmd.ExecuteNonQuery();
                cn.Close();    
                }
                else
                {
                    SqlCeConnection cn = new SqlCeConnection(C.ConnectString());
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    } 
                    String sql = "UPDATE Data SET Adress=@Adress, City=@City, PCode=@PCode, State=@State, Country=@Country, Tel=@Tel, Cell=@Cell, EMail=@EMail WHERE Id= '" + lblID.Text + "'";
                    SqlCeCommand cmd = new SqlCeCommand(sql, cn);
                    cmd.Parameters.Add(new SqlCeParameter("@Adress", SqlDbType.NVarChar, 256));
                    cmd.Parameters.Add(new SqlCeParameter("@City", SqlDbType.NVarChar, 50));
                    cmd.Parameters.Add(new SqlCeParameter("@PCode", SqlDbType.NVarChar, 15));
                    cmd.Parameters.Add(new SqlCeParameter("@State", SqlDbType.NVarChar, 50));
                    cmd.Parameters.Add(new SqlCeParameter("@Country", SqlDbType.NVarChar, 50));
                    cmd.Parameters.Add(new SqlCeParameter("@Tel", SqlDbType.NVarChar, 25));
                    cmd.Parameters.Add(new SqlCeParameter("@Cell", SqlDbType.NVarChar, 25));
                    cmd.Parameters.Add(new SqlCeParameter("@EMail", SqlDbType.NVarChar, 256)); 
                    cmd.Parameters["@Adress"].Value = t3.Text;
                    cmd.Parameters["@City"].Value = t4.Text;
                    cmd.Parameters["@PCode"].Value = t5.Text;
                    cmd.Parameters["@State"].Value = t6.Text;
                    cmd.Parameters["@Country"].Value = t7.Text;
                    cmd.Parameters["@Tel"].Value = t8.Text;
                    cmd.Parameters["@Cell"].Value = t9.Text;
                    cmd.Parameters["@EMail"].Value = t10.Text; 
                    cmd.ExecuteNonQuery();
                    cn.Close();   
                }

    je charge de cette facon dans un datagridview

    lblID.Text = dgvData.CurrentRow.Cells[0].Value.ToString();
                t1.Text = dgvData.CurrentRow.Cells[1].Value.ToString();
                t2.Text = dgvData.CurrentRow.Cells[2].Value.ToString();
                t3.Text = dgvData.CurrentRow.Cells[3].Value.ToString();
                t4.Text = dgvData.CurrentRow.Cells[4].Value.ToString();
                t5.Text = dgvData.CurrentRow.Cells[5].Value.ToString();
                t6.Text = dgvData.CurrentRow.Cells[6].Value.ToString();
                t7.Text = dgvData.CurrentRow.Cells[7].Value.ToString();
                t8.Text = dgvData.CurrentRow.Cells[8].Value.ToString();
                t9.Text = dgvData.CurrentRow.Cells[9].Value.ToString();
                t10.Text = dgvData.CurrentRow.Cells[10].Value.ToString();
    
                using (SqlCeConnection cn = new SqlCeConnection(C.ConnectString()))
                {
                    using (SqlCeCommand cmd = new SqlCeCommand("SELECT Photo FROM Data WHERE Id=" + lblID.Text, cn))
                    {
                        if (cn.State == ConnectionState.Closed) { cn.Open(); }
                        byte[] imageData = (byte[])cmd.ExecuteScalar();
                        if ((imageData != null))
                        {
                            using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                            { 
                                ms.Write(imageData, 0, imageData.Length);  
                                img1.Image = Image.FromStream(ms, false);
                            }
                        }
                    } cn.Close();
                }

    Maintenant ca fonctionne mais je dois la réécrire de facon moin hardcode  a ajouter ceci dans ma class

    MERCI


    Daniel

    dimanche 18 janvier 2015 21:33