473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

drawing with mouse on a panel

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
1 3924
Plater
7,872 Expert 4TB
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

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

Similar topics

2
by: Littlecharva | last post by:
Hi, I want to give the user of my program the ability to draw lines onto a panel, much the same as a paint program. I can draw the lines onto the panel, but I want the line to move around until...
3
by: mitsura | last post by:
Hi, I have included a small listing. The test program opens a panel and show a bitmap. What I want is to when the mouse is over the bitmap panel, I want to trap the left mouse click. The...
1
by: Jay | last post by:
I need to be able to move a Panel on a windows Form at run time using the mouse. I have tried adding a MouseDown event handler to the Panel and then within the MouseDown handler adding a MouseMove...
2
by: Champika Nirosh | last post by:
Hi, I want to create drawing board application that can draw Line, rectagle, circle and free hand drawing. Each drawing need to be transparent, moveable (draggable), have bring to front and...
2
by: Bad_Kid | last post by:
How to make this: I have a panel and I draw something on it (some lines, a complex function...). I want this: When I move mouse over that panel to draw a vertical line from top of the panel to...
11
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
4
by: RobinS | last post by:
I am drawing a rectangle on a picture that has already been drawn on the graphics area (a user control). It works something like this: //in the MouseDown event m_isDragging = true; m_oldX =...
5
by: steve | last post by:
I would like to create a panel, or other widget, on which I can draw, say, a triangle, and use the wheel on the mouse to zoom in and out on the drawing. A vector drawing. I know this can be done...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.