First I need to make some color scribbles (the picture below is taken from M. Yang article's on Still Image Colorization) on an monochrome input image which is loaded into a PictureBox control.
I'm trying to use this to get the effect:
private void PictureBoxOnMouseDown(Object sender, MouseEventArgs e)
{
if((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
this.MouseInitialPosition = e.Location;
}
}
private void PictureBoxOnMouseMove(Object sender, MouseEventArgs e)
{
if((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
this.MouseLastPosition = e.Location;
}
this._PictureBox.Invalidate();
}
private void PictureBoxOnPaint(Object sender, PaintEventArgs e)
{
using(var pen = new Pen(Color.Red, 3.0F))
{
e.Graphics.DrawLine(pen, this.MouseInitialPosition, this.MouseLastPosition);
}
}
But that's giving me not quite I've been waiting for:
I can't put several lines. Lines are not stored;
I'm overwriting line with line;
Second. I need to get all pixels from an image I've been drawing onto and filter them in some way (i.e. extract special ones). How do I store lines/scribbles onto image and then read the image efficiently?
A simple solution would be to store lines when a mouseMove event occurs on a picturebox control and then invalidate to redraw:
public class Lines
{
public Point startPoint = new Point();
public Point endPoint = new Point();
}
Lines l = new Lines();
List<Lines> allLines = new List<Lines>();
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Collect endPoint when mouse moved
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
l.endPoint = e.Location;
//Line completed
allLines.Add(l);
this.pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Collect startPoint when left mouse clicked
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
l = new Lines();
l.startPoint = e.Location;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var aLine in allLines)
{
e.Graphics.DrawLine(Pens.Blue, aLine.startPoint, aLine.endPoint);
}
}
Just keep a list of lines. Then when OnPaint is called draw all the lines.
The Line class would be something like:
public class Line
{
public List<Point> Points = new List<Point>();
public DrawLine(Pen pen, Grphics g)
{
for (int i = 0; i < Points.Count - 1; ++i)
g.DrawLine(pen, Points[i], Points[i+1];
}
}
private void PictureBoxOnMouseDown(Object sender, MouseEventArgs e)
{
if((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
var newLine = new Line();
newLine.Points.Add(e.Location);
lines.Add(newLine);
}
}
PictureBoxOnMouseMove(Object sender, MouseEventArgs e)
{
if((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
lines[lines.Count-1].Points.Add(e.Location);
}
}
private void PictureBoxOnPaint(Object sender, PaintEventArgs e)
{
using(var pen = new Pen(Color.Red, 3.0F))
{
foreach (var line in lines)
DrawLine(pen, e.Graphics)
}
}
Related
Hi I want to save 4 mouse positions when I click on button.
Smth like this:
ButtonClick -> 1.MouseClick / Save Mouse Position-> 2.MouseClick / Save Mouse Positon .....
private void button2_Click_1(object sender, EventArgs e)
{
if (!int.TryParse(textBox4.Text, out parsedValue))
{
MessageBox.Show("Wpsiz liczbe");
return;
}
else
{
iset = int.Parse(textBox3.Text);
ms = int.Parse(textBox4.Text);
MouseDownFunction();
}
}
private void MouseDownFunction(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
eqhelmetx = MousePosition.X;
eqhelmety = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqchestx = MousePosition.X;
eqchesty = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqleginsx = MousePosition.X;
eqleginsy = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqbootsx = MousePosition.X;
eqbootsy = MousePosition.Y;
}
}
}
}
}```
I tried but it doesn't work. Thanks for any help
List<Point> MousePositions = new List<Point>(); // list for saving mouse positions
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (MousePositions.Count == 4)
MousePositions.RemoveAt(0); // for saving last 4 positions
MousePositions.Add(e.Location); // when clicking form saving mouse location
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in MousePositions)
MessageBox.Show(item.ToString()); // showing mouse positions
}
i have a problem.
I'm writing a program that writes on it with a stylus.
First, i create a windows form with a panel.
second, this code:
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace testWrite
{
public partial class Form1 : Form
{
Graphics g;
int x = -1;
int y = -1;
bool moving = false;
Pen pen;
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
pen = new Pen(Color.Black, 5);
pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
x = -1;
y = -1;
moving = false;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
moving = true;
}
}
}
I use this app with a Wacom intuos
But the result is not so good because a few words are lost...haizzz
toi tên la trần
quang hieu
hello heloo
especially, when i write fast or the text is small.
when i write in Microsoft Paint, it is very good
What is best way to to write in windows forms with pen-tablet like wacom intuos?
UPDATE 1:
With cmt from TaW.
Thanks for your help. But, that's not what I need...
i was change my code to:
public partial class Form1 : Form
{
List<Point> curPoints = new List<Point>();
List<List<Point>> allPoints = new List<List<Point>>();
public Form1()
{
InitializeComponent();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
// here we should check if the distance is more than a minimum!
curPoints.Add(e.Location);
// let it show
panel1.Invalidate();
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// ToList creates a copy
allPoints.Add(curPoints.ToList());
curPoints.Clear();
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// begin fresh line or curve
curPoints.Clear();
// startpoint
curPoints.Add(e.Location);
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
// here you can use DrawLines or DrawCurve
// current line
if (curPoints.Count > 1) e.Graphics.DrawCurve(Pens.Red, curPoints.ToArray());
// other lines or curves
foreach (List<Point> points in allPoints)
if (points.Count > 1) e.Graphics.DrawCurve(Pens.Red, points.ToArray());
}
}
But nothing better. The result is worse...
I tried to write: "Hello my name is Hieu", but is not run...
Looks like a pen-tablet differs from a mouse when use to write. Because, with mouse i feel that is better in this code...
UPDATE 2:
With code by Idle_Mind. It will be fine if i set pen-tablet:
With setting "Click", it is not OK
How to fix it, i don't want to set "Double Click" to my pen !
Here's my version...worked great for me. You might need to adjust your tablet settings so that it picks up everything correctly:
public partial class FormTablet : Form
{
private Point lastPoint;
private GraphicsPath GP = null;
private List<GraphicsPath> GPs = new List<GraphicsPath>();
public FormTablet()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lastPoint = new Point(e.X, e.Y);
GP = new GraphicsPath();
GP.AddLine(lastPoint, lastPoint);
GPs.Add(GP);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point pt = new Point(e.X, e.Y);
GP.AddLine(lastPoint, pt);
lastPoint = pt;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
GP = null;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (checkBox1.Checked)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}
using(Pen p = new Pen(Color.Black, (int)numericUpDown1.Value))
{
p.LineJoin = LineJoin.Round;
p.MiterLimit = p.Width / 2;
foreach (GraphicsPath path in GPs)
{
if (path.PathPoints.Count() > 2)
{
// draw the path
e.Graphics.DrawPath(p, path);
}
else
{
// just draw a single dot
Rectangle rc = new Rectangle(Point.Round(path.PathPoints[0]), new Size(1, 1));
rc.Inflate((int)numericUpDown1.Value, (int)numericUpDown1.Value);
e.Graphics.FillEllipse(Brushes.Black, rc);
}
}
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
GPs.Clear();
pictureBox1.Invalidate();
}
}
I am attempting to drag Music Notes vertically, up and down a Music Staff. However, rather than a constant drag, I would like the music notes to only be allowed to be dragged onto particular intervals (only specific y-coordinates). For example, in a vertical line, a music note can be dragged on to coordinates (0,0), (0,5) or (0,10).
Below is my relevant code:
private Point MouseDownLocation;
private void Note_MouseDown(object sender, MouseEventArgs e)
{
foreach (MusicNote mn in panel2.Controls.OfType<MusicNote>())
{
if (sender == mn)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
}
}
private void Note_MouseMove(object sender, MouseEventArgs e)
{
foreach(MusicNote mn in panel2.Controls.OfType<MusicNote>())
{
if (sender == mn)
{
if (e.Button == MouseButtons.Left)
{
mn.Top = e.Y + mn.Top - MouseDownLocation.Y;
}
}
}
}
Any help is appreciated. Thank you!
Basically, you need to check if you drag up or drag down
You should want to check the MouseDown.X and compare it to the MouseUp.X (or Y if you want to check vertical direction as well). It is important to note that (0, 0) is the upper left of your screen. So you need to compare the X position from mouse down event to the mouse up event.
here's an example with one label that moves up and down in steps of 10
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (label1.Location.Y > 0 && label1.Location.Y < panel1.Size.Height) // not the most accurate way, but you get the idea
{
mPointDown = new Point(e.X, e.Y);
}
}
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
bool movedUp, movedDown;
if (e.Y == mPointDown.Y)
{
movedUp = movedDown = false;
}
else
{
movedUp = e.Y < mPointDown.Y;
movedDown = !movedUp;
}
if (movedDown)
{
label1.Location = new Point(label1.Location.X, label1.Location.Y + 10);
}
else if (movedUp)
{
label1.Location = new Point(label1.Location.X, label1.Location.Y - 10);
}
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
mouseDownPoint = e.Location;
}
(New to this and playing around with a basic paint application) Ive found detailed instructions to code a flood-fill but as i am new it is very hard to understand every bits of it, and instead of copying, i would like to try to make my own simple(small scale) flood-fill.
Would it be possible to use fillpath as a flood-fill? i would draw paths and use my mouse to determine my x,y, on screen and have the graphicspath find out if it has borders(points from the drawn paths) and if so, fill these paths with a color?
this is what ive come up with but obviously it doesnt work, so how would i go about to make this working?
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Graphics g;
readonly Pen pen = new Pen(Color.Navy, 2);
Point oldCoords;
GraphicsPath graphicsPaths = new GraphicsPath();
bool spaceFound = false;
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Point mousePt = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Right &&
graphicsPaths.IsVisible(mousePt))
{
spaceFound = true;
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (oldCoords.IsEmpty)
graphicsPaths.StartFigure();
else
{
graphicsPaths.AddLine(oldCoords, new Point(e.X, e.Y));
g.DrawPath(pen, graphicsPaths);
}
oldCoords = new Point(e.X, e.Y);
}
else
oldCoords = Point.Empty;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
g.DrawPath(pen, graphicsPaths);
if(spaceFound == true)
{
g.FillPath(Brushes.AliceBlue, graphicsPaths);
}
}
}
}
Yes, this is quite possible; of course you would want to store the path in a List<GraphicsPath> in the MouseUp event to allow for more filled shapes..
You need to correct a few issues in your code:
Set the path.FillMode to Winding
Never cache a Graphics object
Never use control.CreateGraphics()
Don't cache Pens or Brushes
Only draw in the Paint event, unless you do not want the drawing to persist
The last point might actually apply here: Maybe you don't want the currently drawing outline to stay visible? In that, and only that case you can stick with drawing it in the MouseMove with a Graphics object created there on the fly.
Here is a corrected version:
Point oldCoords;
GraphicsPath graphicsPaths = new GraphicsPath() { FillMode = FillMode.Winding };
bool spaceFound = false;
private void drawPanel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && graphicsPaths.IsVisible(e.Location))
{
spaceFound = true;
drawPanel1.Invalidate();
}
}
private void drawPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (oldCoords.IsEmpty) graphicsPaths.StartFigure();
else
{
graphicsPaths.AddLine(oldCoords, new Point(e.X, e.Y));
drawPanel1.Invalidate();
}
oldCoords = new Point(e.X, e.Y);
}
else oldCoords = Point.Empty;
}
private void drawPanel1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 2f))
e.Graphics.DrawPath(pen, graphicsPaths);
if (spaceFound == true)
{
e.Graphics.FillPath(Brushes.AliceBlue, graphicsPaths);
}
}
Note that it will fill your path but not in the way of a true floodfill, i.e. it will always fill the whole path, not just the innermost segment you have clicked in. For a true floodfill much more involved code is needed that actually goes over all neighbouring pixels starting at the click location..
Examples of a true floodfill are here and here
When user press the left button and move the mouse it should appears a straight line(not permanent line) from previous point to the current mouse moving position. Finally a real straight line will appear when the user releases the left mouse. please help me ..how do i do it?
List<Point> points = new List<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
This is what you're looking for
private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
points.Clear();
points.Push(e.Location);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (points.Count > 1)
{
points.Pop();
}
if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
points.Push(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
I used Stack for ease of use, you are free to change to whatever collection of your choice.
To draw several line you can do something like this
private Stack<Line> lines = new Stack<Line>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
lines.Push(new Line { Start = e.Location });
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (lines.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
lines.Peek().End = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var line in lines)
{
e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
}
}
class Line
{
public Point Start { get; set; }
public Point End { get; set; }
}
you can use mentioned code
Point currentPoint = new Point();
private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
}
private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Line line = new Line();
line.Stroke = SystemColors.WindowFrameBrush;
line.X1 = currentPoint.X;
line.Y1 = currentPoint.Y;
line.X2 = e.GetPosition(this).X;
line.Y2 = e.GetPosition(this).Y;
currentPoint = e.GetPosition(this);
paintSurface.Children.Add(line);
}
}