By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,263 Members | 1,502 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,263 IT Pros & Developers. It's quick & easy.

drawing with mouse on a panel

P: 17
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WhiteBoard
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private System.Drawing.Brush m_objBrush;
  15.         private Graphics m_objGraphics;
  16.         private Point lastPoint = Point.Empty;
  17.         private System.Drawing.Drawing2D.GraphicsPath mousePath;
  18.         private int mouseX, mouseY;
  19.         Point mouseDownLocation;
  20.  
  21.         public Form1()
  22.         {
  23.             mousePath = new System.Drawing.Drawing2D.GraphicsPath();
  24.  
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void toolStripNewBoard_Click(object sender, EventArgs e)
  29.         {
  30.  
  31.         }
  32.  
  33.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  34.         {
  35.             mouseDownLocation = new Point(e.X, e.Y);
  36.             mousePath.StartFigure();
  37.             panel1.Focus();
  38.             panel1.Invalidate();
  39.         }
  40.  
  41.         private void panel1_MouseUp(object sender, MouseEventArgs e)
  42.         {
  43.             panel1.Invalidate();
  44.         }
  45.  
  46.         private void panel1_MouseMove(object sender, MouseEventArgs e)
  47.         {
  48.             // Update the mouse path that is drawn onto the Panel.
  49.             mouseX = e.X;
  50.             mouseY = e.Y;
  51.             if (e.Button == MouseButtons.Left && e.Button == Button.MouseButtons)
  52.             {
  53.                 mousePath.AddLine(mouseX, mouseY, mouseX, mouseY);
  54.                 panel1.Invalidate();
  55.             }
  56.         }
  57.  
  58.         private void panel1_Paint(object sender, PaintEventArgs e)
  59.         { 
  60.             e.Graphics.DrawPath(System.Drawing.Pens.Black, mousePath);
  61.         }
  62.  
  63.  
  64.         private void Form1_Load(object sender, EventArgs e)
  65.         {
  66.             m_objGraphics = this.CreateGraphics();
  67.         }
  68.  
  69.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  70.         {
  71.             m_objGraphics.Dispose();
  72.         }
  73.  
  74.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  75.         {
  76.             if (e.Button != MouseButtons.Left) return;
  77.             m_objGraphics.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, 2, 2);
  78.         }
  79.  
  80.     }
  81. }
  82.  
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!
Feb 22 '08 #1
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
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)
Feb 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.