Is it possible to save the contents of a panel in a binary file?

2

Regarding my question, is it possible to save the contents of a panel in a binary file?

What I need is to know if it is possible and how I could develop it. In other words, I have a panel on which I can draw. After drawing I would like to be able to save what was drawn in a binary file, which can then be mounted and read by another program.

This is in C # winform

Save the panel but not the contents of the Panel (pnl_Draw):

 //Incializa un componente SaveFileDialog.
            SaveFileDialog saveFileDialog = new SaveFileDialog();
    //Cuando buscas archivos te muestra todos los .bmp.
    saveFileDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            //Titulo
            saveFileDialog.Title = "Guardar gráfico como imagen";
        // preguntamos si elegiste un nombre de archivo.
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            //Extención del archivo por defecto segun el filtro del saveFileDialog
            switch (saveFileDialog.FilterIndex)
            {
                case 1:
                    saveFileDialog.DefaultExt = "jpg";
                    break;

                case 2:
                    saveFileDialog.DefaultExt = "bmp";
                    break;

                case 3:
                    saveFileDialog.DefaultExt = "gif";
                    break;
            }

            //Obtenemos alto y ancho del panel
            int width = pnl_Draw.Width;
            int height = pnl_Draw.Height;
            //Inicializamos un objeto BitMap con las dimensiones del Panel
            Bitmap bitMap = new Bitmap(width, height);
            //Inicializamos un objeto Rectangle en la posicion 0,0 y con dimensiones iguales a las del panel.
            //0,0 y las mismas dimensiones del panel porque queremos tomar todo el panel
            // o si solo queremos tomar una parte pues podemos dar un punto de inicio diferente y dimensiones distintas.
            Rectangle rec = new Rectangle(0, 0, width, height);
            //Este metodo hace la magia de copiar las graficas a el objeto Bitmap
            pnl_Draw.DrawToBitmap(bitMap, rec);
            // Y por ultimo salvamos el archivo pasando como parametro el nombre que asignamos en el saveDialogFile
            bitMap.Save(saveFileDialog.FileName);

Thanks to Leandro I could see that I was using the wrong method. To save the content you must draw with Paint

    
asked by Alejandro Maisonnat 20.01.2016 в 23:53
source

1 answer

3

You could retrieve the control image using DrawToBitmap ()

Control.DrawToBitmap Method

Once you have the image you can record an arch with the Save () method

Bitmap bmp = new Bitmap(Panel1.ClientRectangle.Width, Panel1.ClientRectangle.Height);
Panel1.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));

bmp.Save("image.png", ImageFormat.Png);

Saving image to file

    
answered by 21.01.2016 / 00:04
source