Basically I have a form with no border, I want to keep it like that. It's in a fixed position. I'm trying to draw an outline of the form size in the form (so it looks like a border) I'm having trouble and never really used "drawing" techniques in forms.
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
Graphics g = CreateGraphics();
g.DrawRectangle(pen, rect);
Do it in OnPaint:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
e.Graphics.DrawRectangle(pen, rect);
}
Related
I am drawing a rectangle on a image which is within a picture box. The problem I have is that the rectangle is drawn behind the image. Please see the picture attached.
How can I draw on top of the image?
My on paint of the picture is as below. My paint is on my picturebox - I wonder if that is the problem ? but there is no paint on the image?
Rectangle ZoomRect1 = new Rectangle(Math.Min(ZoomToRectangleLeftButtonLocation.X, ZoomToRectangleCurrentButtonLocation.X),
Math.Min(ZoomToRectangleLeftButtonLocation.Y, ZoomToRectangleCurrentButtonLocation.Y),
Math.Abs(ZoomToRectangleLeftButtonLocation.X - ZoomToRectangleCurrentButtonLocation.X),
Math.Abs(ZoomToRectangleLeftButtonLocation.Y - ZoomToRectangleCurrentButtonLocation.Y));
Graphics g1 = e.Graphics;
Pen pen = new Pen(Color.Red, 2);
g1.DrawRectangle(pen, ZoomRect1);
pen.Dispose();
I believe you are using OnPaint method of the form ! Not the pictureBox and that's why the rectangle is in form.
To draw rec on PictureBox you have to do like :
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle ee = new Rectangle(10, 10, 30, 30);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, ee);
}
}
And here the graphics e is for the pictureBox not the form itself.
EDIT
If the first answer didn't help : try this out.
Add this method where ever you want and call it
public void paintOnPictureBox()
{
Rectangle ee = new Rectangle(10, 10, 50, 50);
Graphics gr = Graphics.FromImage(pictureBox1.Image);
using (Pen pen = new Pen(Color.Green, 2))
{
gr.DrawRectangle(pen, ee);
}
}
DON'T FORGET : on pictureBox OnPaint event, add this line :
this.Refresh();
and the results :
EDIT 2 :
It's bad to add
this.Refresh();
in the onPaint method since it may cause other components to flickering. As it slows the form on showing and operating !
It's better to add it in the end of painting method like :
public void paintOnPictureBox()
{
Rectangle ee = new Rectangle(10, 10, 50, 50);
Graphics gr = Graphics.FromImage(pictureBox1.Image);
using (Pen pen = new Pen(Color.Green, 2))
{
gr.DrawRectangle(pen, ee);
}
this.Refresh();
}
I guess you are setting image source to control
Try to draw image, than rectangle
Graphics g1 = e.Graphics;
g1.DrawImage(...);
Pen pen = new Pen(Color.Red, 2);
g1.DrawRectangle(pen, ZoomRect1);
pen.Dispose();
I am trying to draw a Rectangle on a PictureBox, but when I click on another location the Rectangle moves to the new location. I would like to keep the original Rectangle and draw a new Rectangle at the given coordinates.
private void PictureBox_Paint(...)
Pen pen = new Pen(Color.Red, 3);
Rectangle rect = new Rectangle(x - 20, y - 10, 40, 20);
e.Graphics.DrawRectangle(pen, rect);
You need to add your Rectangles to a list of rectangles.
To do so just replace :
OnClick
Rectangle rect = new Rectangle(x - 20, y - 10, 40, 20);
Draw
e.Graphics.DrawRectangle(pen, rect);
With :
OnClick
myRectangleCollection.Add(new Rectangle(x - 20, y - 10, 40, 20));
Draw
foreach(Rectangle rect in myRectangleCollection)
{
e.Graphics.DrawRectangle(pen, rect);
}
Just don't forget to initialize your List of rectangles in your form initialization with this :
List<Rectangle> myRectangleCollection = new List<Rectangle>();
I used
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
to shape the border of rectangle but now I only need to show the corner of that rectangle.
You can draw it by yourself by DrawLine function in Paint event handler, something like this:
Pen pen = new Pen(Color.Red);
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(pen, 0, 0, pictureBox1.Right, 0);
g.DrawLine(pen, 0, 0, 0, pictureBox1.Bottom);
}
It's a use case, maybe you need other coordinates, but you can fix it easily.
You could use 2 lines to get the effect you want:
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(pen, 0, 0, 50, 0 );
e.Graphics.DrawLine(pen, 0, 0, 0, 50);
}
This draws the corner of a rectangle in the top left corner of the form.
I have created some Ellipses on Windows form
I want that selectable while mouse over and if i select any ellipse it will delete from Window
Here some code for create ellipse in c#
public void DrawCircle_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 3); Graphics gr = this.CreateGraphics(); gr.DrawEllipse(pen, 40, 45, 20, 20);
Pen pen2 = new Pen(Color.Black, 3); Graphics gr1 = this.CreateGraphics(); gr.DrawEllipse(pen2, 30, 25, 38, 20);
Pen pen3 = new Pen(Color.Black, 3); Graphics gr2 = this.CreateGraphics(); gr.DrawEllipse(pen3, 35, 36, 68, 15);
Pen pen4 = new Pen(Color.Black, 3); Graphics gr3 = this.CreateGraphics(); gr.DrawEllipse(pen4, 50, 60, 67, 35);
}
You could use
this.Invalidate();
That should clear them....
Either that, or
Graphics.Clear();
Edit:
Actually, another method be to have a boolean inside of your mouse on hover, and use that to dictate when to draw the elipse/not. As a general note, you should be putting the elipse drawing in an override of onpaint. Right now, currently, if you were to invalidate the screen (either by you, or by Windows), your shapes will disappear. An Example using on paint.
bool paint = false;
protected override void OnPaint(object sender, PaintEventArgs e)
{
if (paint)
{
Pen pen = new Pen(Color.Black, 3); Graphics gr = this.CreateGraphics(); gr.DrawEllipse(pen, 40, 45, 20, 20);
Pen pen2 = new Pen(Color.Black, 3); Graphics gr1 = this.CreateGraphics(); gr.DrawEllipse(pen2, 30, 25, 38, 20);
Pen pen3 = new Pen(Color.Black, 3); Graphics gr2 = this.CreateGraphics(); gr.DrawEllipse(pen3, 35, 36, 68, 15);
Pen pen4 = new Pen(Color.Black, 3); Graphics gr3 = this.CreateGraphics(); gr.DrawEllipse(pen4, 50, 60, 67, 35);
}
}
I don't remember the specific paramters for it, but in your OnMouseHover (where you want to turn off your elipse)
OnMouseHover(){
...
paint = false;
this.Invalidate();
}
I have the following paint event (form) that is drawing the rectangle:
void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
Rectangle rect = new Rectangle(100, 100, 400, 100);
Graphics c = rtbLogicCode.CreateGraphics();
c.DrawRectangle(new Pen(Color.Black, 3), rect);
}
The rectangle is shown for a brief moment and then disappears immediately. The rectangle will only show momentarily again when the user resizes the form.
How can I solve this issue?
Don't use the Control.CreateGraphics() method, use the PaintEventArgs.Graphics property:
void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
Rectangle rect = new Rectangle(100, 100, 400, 100);
e.Graphics.DrawRectangle(Pens.Black, rect);
}