Search code examples
c#bitmappaneldrawpaint

C# paint program won't create any lines on the screen since I tried to draw using a bitmap


Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...

namespace TV_PAINT
{
    public partial class ALANA_PAINT : Form
    {
        Graphics g;
        Pen p = new Pen(Color.Black, 7);
        Point sp = new Point(0, 0);
        Point ep = new Point(0, 0);
        int m = 0;
        Bitmap BP;

        public ALANA_PAINT()
        {
            InitializeComponent();
            tb1.Text = p.Width.ToString();
            BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            pnl1.Dispose();
            p.Dispose();
            this.Close();
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            //pnl1.Invalidate();
            p.Color = System.Drawing.Color.Black;
            p.Width = 7;
            tb1.Text = p.Width.ToString();
            //pnl1.Invalidate();
        }

        private void pnl1_MouseDown(object sender, MouseEventArgs e)
        {
            sp = e.Location;
            if (e.Button == MouseButtons.Left)
                m = 1;
            if (e.Button == MouseButtons.Right)
                m = 1;
        }

        private void pnl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m == 1)
            {
                ep = e.Location;
                //g = pnl1.CreateGraphics();
                Graphics g = Graphics.FromImage(BP);
                g.DrawLine(p, sp, ep);
            }
            sp = ep;
        }

        private void pnl1_MouseUp(object sender, MouseEventArgs e)
        {
            m = 0;
        }

Solution

  • BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it. You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx

    Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.

    System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
    System.Drawing.Graphics formGraphics;
    formGraphics = this.CreateGraphics();
    formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
    myBrush.Dispose();
    formGraphics.Dispose();
    

    Updated: If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.