Upload image to picturebox and write to database

Asked

Viewed 767 times

2

I am now in the movies part of the video club, where I have these fields.

inserir a descrição da imagem aqui

And I wanted to upload the image to the Pictura box (it is already working) and when I made new it was saved in the database (I created a cover field of type image). It is possible?

I tried to do as I do for txtboxs:

cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);

Putting instead of the txtClassIMDB.text' tenteipicturebox1.image`. But obviously it doesn’t work.

  • Daniel, it is not necessary to inform language in the title, the tag already identifies the language in your question.

  • OK. You don’t happen to know how to help me with this problem?

  • 1

    @Danielsousa, I believe Thomas' answer will solve your problem. But it’s a tip here that not always this is a good solution.

  • @Georgewurthmann very interesting, thank you for supplementing the reply

3 answers

4

To save a BD photo, one of the means I know, is to transform the photo into an Array of byte, for this I use the method below:

          if (Pic.Image != null)
          {
               using (MemoryStream stream = new MemoryStream())
               {
                    Pic.Image.Save(stream, ImageFormat.Jpeg);                                        

                    byte[] Bfoto = stream.ToArray();

                    Classes.Cadastro.Crm.Analise_CRM Cad_Foto = new Classes.Cadastro.Crm.Analise_CRM();

                    Cad_Foto.Cad_Foto_Anal_Crm(textEdit8.Text, Bfoto, Pic.Name, Pic.Properties.ZoomPercent);
                }

                 Pic.Image.Dispose();
                 Pic.Image = null;
          }

Now, in the database, use [varbinary] in the column where you save the photos:

CREATE TABLE [dbo].[Crm_Fotos]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[Bfoto] [varbinary](max) NULL,
[item] [varchar](150) NULL,
[foto_seq] [varchar](200) NULL
(

Ai just pass the parameter like any other data, only difference is using:

cmd.Parameters.Add("@Bfoto", SqlDbType.VarBinary).Value = Bfoto;

Questions with answers that can help:

To return multiple rows with sql Server byte array

Convert and save photo in BD

It is wrong to write byte of images in the database?

  • I’ll try, thank you!

  • textEdit8.Text - This part is required to have?

  • Another question I put this code after this:

1


Code I used and worked:

                FileStream Stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
            BinaryReader binary = new BinaryReader(Stream);
            img = binary.ReadBytes((int)Stream.Length);
            if (txtNameMovie.Text != "" && txtIDMovie.Text != "")
            {
                sqlCon.Open();
                SqlCommand cmd = sqlCon.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO Filmes(IdFilme,Titulo,Realizador,Genero,Ano,ClassifIdade,ClassfImdb,Formato,Capa) values(@id,@titulo,@realizador,@categoria,@ano,@classidade,@classimdb,@formato,@capa)";
                cmd.Parameters.AddWithValue("@id", txtIDMovie.Text);
                cmd.Parameters.AddWithValue("@titulo", txtNameMovie.Text);
                cmd.Parameters.AddWithValue("@realizador", txtRealizador.Text);
                cmd.Parameters.AddWithValue("@categoria", txtCatMovies.Text);
                cmd.Parameters.AddWithValue("@ano", txtAnoMovie.Text);
                cmd.Parameters.AddWithValue("@classidade", txtClassIdade.Text);
                cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);
                cmd.Parameters.AddWithValue("@formato", cbFormato.Text);
                cmd.Parameters.Add(new SqlParameter("@capa", img));
                int x = cmd.ExecuteNonQuery();
                sqlCon.Close();
                disp_data();
                ClearData();
                MessageBox.Show("Filme gravado com sucesso!", "Operação Realizada com Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

0

   if (Pic.Image != null)
      {
           using (MemoryStream stream = new MemoryStream())
           {
                Pic.Image.Save(stream, ImageFormat.Jpeg);                                        

                byte[] Bfoto = stream.ToArray();

                Classes.Cadastro.Crm.Analise_CRM Cad_Foto = new Classes.Cadastro.Crm.Analise_CRM();

                Cad_Foto.Cad_Foto_Anal_Crm(textEdit8.Text, Bfoto, Pic.Name, Pic.Properties.ZoomPercent);
            }

             Pic.Image.Dispose();
             Pic.Image = null;
      }

That part goes after:

            sqlCon.Open();
            SqlCommand cmd = sqlCon.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO Filmes(IdFilme,Titulo,Realizador,Genero,Ano,ClassifIdade,ClassfImdb,Capa) values(@id,@nome,@realizador,@categoria,@ano,@classidade,@classimdb,@capa)";
            cmd.Parameters.AddWithValue("@id", txtIDMovie.Text);
            cmd.Parameters.AddWithValue("@nome", txtNameMovie.Text);
            cmd.Parameters.AddWithValue("@realizador", txtRealizador.Text);
            cmd.Parameters.AddWithValue("@categoria", txtCatMovies.Text);
            cmd.Parameters.AddWithValue("@ano", txtAnoMovie.Text);
            cmd.Parameters.AddWithValue("@classidade", txtClassIdade.Text);
            cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.