Serialize image in c #

1

My problem is that when I click on the open button I want it to open automatically, it can be seen, and serialized with the * .poo path in the same location where I opened the image, all this only with the button open, since I want to delete the deserialize button.

Any suggestions on how to do that?

private void btnAbrir_Click(object sender, EventArgs e)
        {
            OpenFileDialog ventana = new OpenFileDialog();
            ventana.Filter = "Archivo JPG|*.jpg|Archivo PNG|*.png";

            if (ventana.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.Image = System.Drawing.Bitmap.FromFile(ventana.FileName);
            }  
        }

        private void btnserializar1_Click(object sender, EventArgs e)
        {
            SaveFileDialog ventana = new SaveFileDialog();
            ventana.Filter = "Archivo Binario|*.poo";

            if(ventana.ShowDialog()==DialogResult.OK)
            {
                FileStream fichero = new FileStream(ventana.FileName,FileMode.Create);

                BinaryFormatter formatoBinario = new BinaryFormatter();
                formatoBinario.Serialize(fichero, this.pictureBox1.Image);

                fichero.Close();

                MessageBox.Show("Serializado Okey");
            }

        }

        private void btndeserializar1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ventana = new OpenFileDialog();
            ventana.Filter = "Archivo Binario|*.poo";

            if (ventana.ShowDialog() == DialogResult.OK)
            {
                FileStream fichero = new FileStream(ventana.FileName, FileMode.Open);

                BinaryFormatter formatoBinario = new BinaryFormatter();


                this.pictureBox1.Image = (Bitmap)formatoBinario.Deserialize(fichero);

                fichero.Close();

                MessageBox.Show("Deserializado Okey");
            }
        }
    
asked by hellen trejo 08.10.2018 в 19:15
source

1 answer

0

I think the main thing to do what you want, is change the extension of the file name, for this, you can use the function ChangeExtension of the class Path , that you would use it in the following way:

string nuevoNombre = Path.ChangeExtension(ventana.FileName, ".poo");

Where ventana is the variable of type OpenFileDialog that you already had declared.

To get your bearings, an example similar to what you want to do would be:

OpenFileDialog ventana = new OpenFileDialog();
ventana.Filter = "Archivo JPG|*.jpg|Archivo PNG|*.png";

if (ventana.ShowDialog() == DialogResult.OK) {
    //acá carga la imagen al pictureBox
    nuevoNombre = Path.ChangeExtension(ventana.FileName, ".poo");
    FileStream fichero = new FileStream(nuevoNombre, FileMode.Create); //acá usas el nombre nuevo
    //Y ya sigues con el proceso para serializar...
}  

I hope that with the example you can orient yourself, it's up to you to put the pieces of the puzzle together to make the final solution.

    
answered by 09.10.2018 / 20:14
source