In my Form, I have 2 picturebox controls. I loaded a blue background image on pictureBox1 and left pictureBox2 control alone. With the code below I'm able to draw arrows on top of my picturebox1 image.
Goal: On my pictureBox1_MouseUp event I want to add all the arrows which I drew on pictureBox1 to pictureBox2.
Issue: The problem is on my pictureBox1_MouseUp event when I wrote pictureBox2.Image = pictureBox1.Imageit doesn't add the painted arrow which I drew on pictureBox1. It only adds the pictureBox1 image that I assigned in my form load event.
private bool isMoving = false;
private Point mouseDownPosition = Point.Empty;
private Point mouseMovePosition = Point.Empty;
private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
Pen _Pen;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (isMoving)
{
if (pictureBox1.Image == null) e.Graphics.Clear(Color.White);
// Add this line for high quality drawing:
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
_Pen = new Pen(Color.IndianRed, 3);
_Pen.CustomEndCap = bigArrow;
e.Graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition);
_Pen.Dispose();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMoving = true;
mouseDownPosition = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
mouseMovePosition = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
}
isMoving = false;
pictureBox2.Image = pictureBox1.Image;
}
Test 1: (Changed pictureBox1_Paint code)
With this code, it draws the arrow on pictureBox2, but it looks like it is drawing multiple arrows.
if (isMoving)
{
if (pictureBox1.Image == null) e.Graphics.Clear(Color.White);
// Add this line for high quality drawing:
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
_Pen = new Pen(Color.IndianRed, 3);
Bitmap BitImg = (Bitmap)pictureBox1.Image;
_Pen.CustomEndCap = bigArrow;
using (var graphics = Graphics.FromImage(BitImg))
{
graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition);
}
pictureBox1.Image = BitImg;
_Pen.Dispose();
}
Test 2: (I took the code from the paint event and pasted it MouseMove Event with some modifications. This uses too much memory and it doesn't draw on pictureBox1 but the arrow is now visible in pictureBox2)
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
mouseMovePosition = e.Location;
if (isMoving)
{
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
_Pen = new Pen(Color.IndianRed, 3);
BitImg = new Bitmap(pictureBox1.Image);
_Pen.CustomEndCap = bigArrow;
using (var graphics = Graphics.FromImage(BitImg))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition);
}
_Pen.Dispose();
}
pictureBox1.Invalidate();
}
}
Draw all lines to pictureBox1:
Draw only last line to pictureBox1:
In pictureBox2 control, add Paint event to pictureBox2_Paint.
I suggest you make pen and cap global varriable:
// Make pen and cap global varriable to boost the perfomane.
// Create and delete them each draw will cost alot of CPU
Pen pen = new Pen(Color.IndianRed, 3);
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
In Form1() event, add this line:
pen.CustomEndCap = bigArrow;
and do as following:
public partial class Form1 : Form
{
private bool isMoving = false;
private Point mouseDownPosition = Point.Empty;
private Point mouseMovePosition = Point.Empty;
private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
public Form1()
{
InitializeComponent();
pen.CustomEndCap = bigArrow;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMoving = true;
mouseDownPosition = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
mouseMovePosition = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
pictureBox2.Invalidate();
}
isMoving = false;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (isMoving)
{
if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White);
// Add this line for high quality drawing:
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawLine(pen, mouseDownPosition, mouseMovePosition);
// If you want draw all previous lines here, add bellow code:
//foreach (var line in lines)
//{
// e.Graphics.DrawLine(pen, line.Item1, line.Item2);
//}
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
foreach (var line in lines)
{
e.Graphics.DrawLine(pen, line.Item1, line.Item2);
}
}
}
Above code draw lines to PictureBox control, not to image, this allow you remove some lines or clear all lines you draw to picturebox if you want later.
If you want draw directly to image, things event much easier, you don't need pictureBox2_Paint at all:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
// You event don't need this line
//lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
if (pictureBox1.Image != null)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawLine(pen, mouseDownPosition, mouseMovePosition);
}
pictureBox2.Image = pictureBox1.Image;
}
}
isMoving = false;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (isMoving)
{
if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawLine(pen, mouseDownPosition, mouseMovePosition);
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
}
You have two problems here:
You paint on Control rather than an image. The image remains unchanged.
Both pictureBox1 and pictureBox2 refer to the same image. When you change the image, both controls will be affected on the next paint event.
So in pictureBox1_Paint you need create a copy of pictureBox1.Image (try using Bitmap), then paint on it and assign the updated image to pictureBox1.Image. It will be painted automatically.
Related
In my form, I have a picturebox. I wanted to be able to draw arrows on top of an image. I managed to get halfway there. On the Form load event, I assign an image to the picturebox. I'm able to create arrows with the code below. The problem is every time I create the arrow the picture I assigned on my form load event gets erased. Why does my image get erased? How do I maintain my image that I assigned on form load while drawing arrows on top of it?
private bool isMoving = false;
private Point mouseDownPosition = Point.Empty;
private Point mouseMovePosition = Point.Empty;
private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
Pen _Pen;
private void Test_Load(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.background;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (isMoving)
{
if (pictureBox1.Image == null)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
}
pictureBox1.Image = bmp;
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.Clear(pictureBox1.BackColor);
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
_Pen = new Pen(Color.IndianRed, 3);
_Pen.CustomEndCap = bigArrow;
g.DrawLine(_Pen, mouseDownPosition, mouseMovePosition);
_Pen.Dispose();
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMoving = true;
mouseDownPosition = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
mouseMovePosition = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
}
isMoving = false;
}
I think the problem is with this line here g.Clear(pictureBox1.BackColor);
Yes, problem is with this line here g.Clear(pictureBox1.BackColor); You erase entire the control area before you draw the line.
You should draw to e.Graphics directly:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (isMoving)
{
if (pictureBox1.Image == null) e.Graphics.Clear(Color.White);
// Add this line for high quality drawing:
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
_Pen = new Pen(Color.IndianRed, 3);
_Pen.CustomEndCap = bigArrow;
e.Graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition);
_Pen.Dispose();
}
}
I'm using WinForms. In my form i have a picturebox with an image. How can i paint the picturebox but not the area inside the expanding square. Here is my code. Currently i could create the expanding square but i don't know how to paint the picturebox white outside of that square.
int _cropX, _cropY, _cropWidth, _cropHeight;
private State _currentState;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
Cursor = Cursors.Cross;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//X and Y are the coordinates of Crop
pictureBox1.Refresh();
_cropWidth = e.X - _cropX;
_cropHeight = e.Y - _cropY;
pictureBox1.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _cropWidth, _cropHeight);
}
}
}
else
{
Cursor = Cursors.Default;
}
}
private void Crop_Checkbox_CheckedChanged(object sender, EventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
this.Cursor = Cursors.Cross;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
_cropX = e.X;
_cropY = e.Y;
_cropPen = new Pen(Color.FromArgb(153, 180, 209), 3); //2 is Thickness of line
_cropPen.DashStyle = DashStyle.DashDotDot;
pictureBox1.Refresh();
}
}
}
else
{
Cursor = Cursors.Default;
}
}
public Pen _cropPen;
private enum State
{
Crop
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
//Paint picturebox...
}
else
{
Cursor = Cursors.Default;
}
}
This is best done with a GraphicsPath:
using System.Drawing.Drawing2D;
..
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle r1 = pictureBox1.ClientRectangle; // note I don't use width or height!
Rectangle r2 = new Rectangle(50, 30, 80, 40);
GraphicsPath gp = new GraphicsPath(FillMode.Alternate);
gp.AddRectangle(r1); // first the big one
gp.AddRectangle(r2); // now the one to exclude
e.Graphics.FillPath( Brushes.Gold, gp);
}
Note that I..
..use the Paint event for persistent grphics
..only paint onto the surface of the PictureBox, not into its image. See here for the difference!
You can add more rectangles or other shapes to exclude.
If you want image and surface combined, either draw into the image or ask the PictureBox to DrawToBitmap..
I'm trying to take a partial screenshot of the screen and put it in a picturebox. The user has to draw a rectangle and then i have to get the portion inside the rectangle.
protected override void OnMouseDown(MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if( e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
using(Pen pen = new Pen(Color.Red, 1))
{
e.Graphics.DrawRectangle(pen, mRect);
}
}
With this code i can draw a recangle, but i don't know how to do the screenshot. Maybe with the fonction CopyToScreen?
If someone have an idea?
Thanks!
Here is some code I use - it grabs the whole desktop
public static Bitmap GetScreenAsBmp()
{
int min_x=0, max_x=0, min_y=0, max_y=0;
foreach (Screen s in Screen.AllScreens)
{
min_x = System.Math.Min(min_x, s.Bounds.Left);
max_x = System.Math.Max(max_x, s.Bounds.Right);
min_y = System.Math.Min(min_y, s.Bounds.Top);
max_y = System.Math.Max(max_y, s.Bounds.Bottom);
}
Bitmap bmp = new Bitmap(max_x - min_x, max_y - min_y);
Graphics dst_dc = Graphics.FromImage(bmp);
dst_dc.CopyFromScreen(min_x, min_y, 0, 0, new Size(max_x - min_x, max_y - min_y));
dst_dc.Dispose();
return bmp;
}
This is a complete solution for the problem.
It is triggered by a Button and then:
creates and styles a second Form which
is transparent without borders etc and fills the screen
has all events needed to draw a Rectangle
copies the screen after the rectangle is drawn, i.e. after the MouseUp
and copies the Bitmap into the Image of a PictureBox on the 1st Form.
Finally it closes the 2nd form. During the screencopy process both windows are hidden, so you can grab the screen itself with all its content.
Note that many video overlays can't be captured this way!
Note that this code always captures from the 1st screen!
For best usability the users should somehow be informed that he is expected to draw the frame after the Button is pressed and the window has disappeared!
To use it simply add a Button and a PictureBox to your Form..
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form form2;
Point MD = Point.Empty;
Rectangle rect = Rectangle.Empty;
private void button2_Click(object sender, EventArgs e)
{
Hide();
form2 = new Form();
form2.BackColor = Color.Wheat;
form2.TransparencyKey = form2.BackColor;
form2.ControlBox = false;
form2.MaximizeBox = false;
form2.MinimizeBox = false;
form2.FormBorderStyle = FormBorderStyle.None;
form2.WindowState = FormWindowState.Maximized;
form2.MouseDown += form2_MouseDown;
form2.MouseMove += form2_MouseMove;
form2.Paint += form2_Paint;
form2.MouseUp += form2_MouseUp;
form2.Show();
}
void form2_MouseDown(object sender, MouseEventArgs e)
{
MD = e.Location;
}
void form2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button !=MouseButtons.Left) return;
Point MM = e.Location;
rect = new Rectangle(Math.Min(MD.X, MM.X), Math.Min(MD.Y, MM.Y),
Math.Abs(MD.X - MM.X), Math.Abs(MD.Y - MM.Y));
form2.Invalidate();
}
void form2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Red, rect);
}
void form2_MouseUp(object sender, MouseEventArgs e)
{
form2.Hide();
Screen scr = Screen.AllScreens[0];
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.CopyFromScreen(rect.Location, Point.Empty, rect.Size,
CopyPixelOperation.SourceCopy);
pictureBox1.Image = bmp;
}
form2.Close();
Show();
}
}
I want to draw a continous line on a picturebox and also a cross located at the cursor. How can it be done to paint the cursor only at the mouse position (and clear it on the next move) and retain the line?
This is just to give you an idea of how to approach this...
Add a PictureBox to a Form. Run it and click and drag in the PictureBox:
public partial class Form1 : Form
{
Point cursor;
Point lastPoint;
System.Drawing.Drawing2D.GraphicsPath curPath = null;
private List<System.Drawing.Drawing2D.GraphicsPath> paths = new List<System.Drawing.Drawing2D.GraphicsPath>();
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (System.Drawing.Drawing2D.GraphicsPath gp in paths)
{
e.Graphics.DrawPath(Pens.Red, gp);
}
e.Graphics.DrawLine(Pens.Black, new Point(cursor.X, 0), new Point(cursor.X, pictureBox1.Height));
e.Graphics.DrawLine(Pens.Black, new Point(0, cursor.Y), new Point(pictureBox1.Width, cursor.Y));
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
lastPoint = new Point(e.X, e.Y);
curPath = new System.Drawing.Drawing2D.GraphicsPath();
paths.Add(curPath);
}
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
cursor = new Point(e.X, e.Y);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point newPoint = new Point(e.X, e.Y);
curPath.AddLine(lastPoint, newPoint);
lastPoint = newPoint;
pictureBox1.Refresh();
}
pictureBox1.Refresh();
}
}
i just wanted to put a selection on my picturebox.image but this has just become worse than some little annoying situation. I thought on another picture box over the main picturebox but it seemed so lazy work to me. I need to know if there is a way to create a selection area (which is gonna be half transparent blue area) on a picturebox.image which im gonna draw with mouse and it shouldnt change the image im working on.
sample:
// Start Rectangle
//
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determine the initial rectangle coordinates...
RectStartPoint = e.Location;
Invalidate();
}
// Draw Rectangle
//
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Point tempEndPoint = e.Location;
Rect =
new Rectangle(
Math.Min(RectStartPoint.X, tempEndPoint.X),
Math.Min(RectStartPoint.Y, tempEndPoint.Y),
Math.Abs(RectStartPoint.X - tempEndPoint.X),
Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
Invalidate(Rect);
}
// Draw Area
//
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Draw the rectangle...
if (pictureBox1.Image != null)
{
Brush brush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));
e.Graphics.FillRectangle(brush, Rect);
}
}
I used your code, you were nearly there. You needed to Invalidate the pictureBox1 instead of the rectangle. I also added a check for the Rect so it doesn't get drawn when it's not initialized or has no size.
Another important change: I created the Rectangle only once and I adjusted its location and size. Less garbage to clean up!
EDIT
I added a mouse right-click handler for the Rectangle.
private Point RectStartPoint;
private Rectangle Rect = new Rectangle();
private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));
// Start Rectangle
//
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determine the initial rectangle coordinates...
RectStartPoint = e.Location;
Invalidate();
}
// Draw Rectangle
//
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Point tempEndPoint = e.Location;
Rect.Location = new Point(
Math.Min(RectStartPoint.X, tempEndPoint.X),
Math.Min(RectStartPoint.Y, tempEndPoint.Y));
Rect.Size = new Size(
Math.Abs(RectStartPoint.X - tempEndPoint.X),
Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
pictureBox1.Invalidate();
}
// Draw Area
//
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Draw the rectangle...
if (pictureBox1.Image != null)
{
if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
{
e.Graphics.FillRectangle(selectionBrush, Rect);
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (Rect.Contains(e.Location))
{
Debug.WriteLine("Right click");
}
}
}
private int xUp, yUp, xDown,yDown;
private Rectangle rectCropArea;
private void SrcPicBox_MouseUp(object sender, MouseEventArgs e)
{
//pictureBox1.Image.Clone();
xUp = e.X;
yUp = e.Y;
Rectangle rec = new Rectangle(xDown,yDown,Math.Abs(xUp xDown),Math.Abs(yUp-yDown));
using (Pen pen = new Pen(Color.YellowGreen, 3))
{
SrcPicBox.CreateGraphics().DrawRectangle(pen, rec);
}
rectCropArea = rec;
}
private void SrcPicBox_MouseDown(object sender, MouseEventArgs e)
{
SrcPicBox.Invalidate();
xDown = e.X;
yDown = e.Y;
}
private void btn_upload_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
// PictureBox SrcPicBox = new PictureBox();
opf.Filter = "ALL images(*.*)|*.*";
if (opf.ShowDialog() == DialogResult.OK)
{
string name = opf.SafeFileName;
string filepath = opf.FileName;
File.Copy(filepath, name, true);
SrcPicBox.Image = Image.FromFile(opf.FileName);
}
private void btn_crop_Click(object sender, EventArgs e)
{
pictureBox3.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
Graphics g = pictureBox3.CreateGraphics();
//Draw the image on the Graphics object with the new dimesions
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
}