473,763 Members | 5,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

drawing with mouse on a panel

17 New Member
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 3985
Plater
7,872 Recognized Expert Expert
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
1609
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 the second time the mouse is clicked before the line is drawn permanently. So I need to know how the save a portion of the panel before I draw the line, so when the mouse moves I can restore the portion (the line disappears) before I draw the...
3
3608
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 purpose is to get the position of the mouse pointer on the bitmap. However, for some reason, the left (I also tried right) mouse clicks are not intercepted. I new to Python and wxWindows so any help would be greatly appreciated.
1
2906
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 event to the Panel and then setting the Panel's Location to a new Point(x, y) with x and y set to the mouse's x and y, but this does not work because the MouseDown event of Panel only sends the cords of the mouse within that Panel. I need to be...
2
12344
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 send to back feature etc. How can I do this .. where should I start?
2
1584
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 panel.height on the X position of the mouse? While I'm moving a mouse over a panel that vertical line is moving too aloong with the mouse cursor. Do I have to redraw a whole panel for every mouse movement??? That's not a
11
11270
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
27005
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 = e.X; //save original positions m_oldY = e.Y; //in the MouseMove event if (m_isDragging)
5
4738
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 because I have seen apps that do this. They were 3D and I only need 2D but they were very fast. Is there an easy way to do this? Do I need to use the DirectX libraries to do this?
7
3560
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 excellent transforms specified on Bob Powell's site, I still wonder if there's an easier way of centering it than the following procedure? Here is what I do now (it's awkward but it works): 1) I follow the transforms specified on Bob Powell's...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.