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: -
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!
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)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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...
|
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?
|
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
| |
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
|
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)
|
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?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
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...
| |