Is it possible to save what is drawn in the "Panel" control using the Paint event?

2

Next I use the following code to save and draw. But when saving does not save what is drawn.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ColorMap
{
    public partial class Form1 : Form
    {
        private SolidBrush myBrush;
        private Graphics myGraphics;
        private bool isDrawing = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myBrush = new SolidBrush(panel2.BackColor);
            myGraphics = panel1.CreateGraphics();
        }

        private void panel2_DoubleClick(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                panel2.BackColor = colorDialog1.Color;
                myBrush.Color = panel2.BackColor;

            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing == true)
            {
                myGraphics.FillEllipse(myBrush, e.X, e.Y, trackBar1.Value, trackBar1.Value);
            }
        }

        private void panel3_DoubleClick(object sender, EventArgs e)
        {
            //Backgroun canvas
            if (colorDialog2.ShowDialog() == DialogResult.OK)
            {
                panel1.BackColor = colorDialog2.Color;
                panel3.BackColor = colorDialog2.Color;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //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 = panel1.Width;
                int height = panel1.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
                panel1.DrawToBitmap(bitMap, rec);
                // Y por ultimo salvamos el archivo pasando como parametro el nombre que asignamos en el saveDialogFile
                bitMap.Save(saveFileDialog.FileName);
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            isDrawing = true;
        }
    }
}

I would like to know 2 things:

  • How could I modify the Paint event so that it activates when doing Click on the Panel?
  • How to record everything that is drawn on the panel?
asked by Alejandro Maisonnat 27.01.2016 в 20:21
source

2 answers

1

> > How you could modify the Paint event to activate when you click on the Panel.

You could put a flag to enable when you should draw

private bool habilitarPaint = false;

public void Panel1_Click(...){
   habilitarPaint  = true;
}

public void Panel1_Paint(...){

   if(!habilitarPaint)
       return;

   //resto codigo
}

But the drawing should be done within the Paint event, analyze the comments here

How can I draw over a panel in C #?

> > How to record everything that is drawn on the panel

In principle the code you define is correct, the DrawToBitmap() is used to recover the drawn image. Although you could indicate an image format using

Image.Save Method (String, ImageFormat)

    
answered by 27.01.2016 / 20:35
source
1

When you call DrawToBitmap , the code behind calls the Paint method in the panel. The difference between that call and the one that occurs by default when the content is displayed on the screen is that the Graphics object that is passed does not go to the screen, but is used to paint in Bitmap .

In your case, the drawings you make are non-persistent drawings, proof of this is that if you force a refresh on the form, for example by minimizing the form or placing another window on top, you will see that the image disappears.

For the drawings to be persistent they have to be painted in the Paint method. Perhaps the simplest thing is that you add code in that method so that it flips over a collection of objects to be managed by your code.

Something like that (it's a suggestion and I can not compile or try it right now):

// Clase base para los objetos a pintar
class Objeto
{
  public Point Location
  { get; set; }

  public Size Size
  { get; set; }

  public Brush Brush
  { get; set; }

  public abstract void Paint(Graphics g);
}

class Elipse : Objeto
{
  public override void Paint(Graphics g)
  {
    g.FillEllipse(Brush, Location, Size);
  }
}

public partial class Form1 : Form
{
  List<Objeto> toPaint = new List<Objeto>();

  private void button_Click(object sender, EventArgs e)
  {
    var elipse = new Elipse();
    elipse.Location = new Point(10,10);
    elipse.Size = new Size(20,10);
    toPaint.Add(elipse);
  }

  private void panel1_Paint(object sender, PaintEventArgs e)
  { 
    isDrawing = true;

    foreach( var elem in toPaint )
      elem.Paint(e.Graphics);
  }
}
    
answered by 27.01.2016 в 20:43