drawing with mouse on a panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YouPoP
    New Member
    • Jun 2007
    • 17

    drawing with mouse on a panel

    I am doing an app (C# 2.0) where you can draw in a panel with your mouse in "real time". I actually have 2 problems;
    1- it does not really is "real time", if your mouse move fast or very fast the line is added after a very small delay.
    2-Because I use AddLine(), it adds very short lines from one point to another and it does not give a very good result. Also because of this and my "not real time" problem, when the mouse moves fast it adds straight lines instead of really following the mouse and drawing curves or whatever you want to draw.

    here is my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WhiteBoard
    {
        public partial class Form1 : Form
        {
            private System.Drawing.Brush m_objBrush;
            private Graphics m_objGraphics;
            private Point lastPoint = Point.Empty;
            private System.Drawing.Drawing2D.GraphicsPath mousePath;
            private int mouseX, mouseY;
            Point mouseDownLocation;
    
            public Form1()
            {
                mousePath = new System.Drawing.Drawing2D.GraphicsPath();
    
                InitializeComponent();
            }
    
            private void toolStripNewBoard_Click(object sender, EventArgs e)
            {
    
            }
    
            private void panel1_MouseDown(object sender, MouseEventArgs e)
            {
                mouseDownLocation = new Point(e.X, e.Y);
                mousePath.StartFigure();
                panel1.Focus();
                panel1.Invalidate();
            }
    
            private void panel1_MouseUp(object sender, MouseEventArgs e)
            {
                panel1.Invalidate();
            }
    
            private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                // Update the mouse path that is drawn onto the Panel.
                mouseX = e.X;
                mouseY = e.Y;
                if (e.Button == MouseButtons.Left && e.Button == Button.MouseButtons)
                {
                    mousePath.AddLine(mouseX, mouseY, mouseX, mouseY);
                    panel1.Invalidate();
                }
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            { 
                e.Graphics.DrawPath(System.Drawing.Pens.Black, mousePath);
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                m_objGraphics = this.CreateGraphics();
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                m_objGraphics.Dispose();
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;
                m_objGraphics.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, 2, 2);
            }
    
        }
    }
    using m_objGraphics and the FillEllipse works fine for drawing on the form only, I have not succeeded using it in the panel yet and it does not seems to be supported by the panel (unless i forgot something). the mousePath works nice in the panel but i do not really want to draw straight lines and would like the drawing really following the mouse.

    Thanks in advance for your help!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What if you had a bitmap object that represented the drawing.
    Whenever the mouse moves (with the button down) you use a .SetPixel() on the bitmap to draw it?
    Then you can just keep refreshing the panel (which has the bitmap as it's background or whatever)

    Comment

    Working...