My question may sound stupid but I wonder if I can draw a rectangle over my webbrowser object and move that constant sized rectangle over my webbrowser?
I have tried to draw a rectangle but rectangle does not visible on my form and webbrowser.
I dont know what is wrong with it.
Graphics g = webBrowser1.CreateGraphics();
Rectangle rect = new Rectangle(400,400, 200, 100);
using (Pen pen = new Pen (Color.Red, 2)
{
g.DrawRectangle(pen, rect);
}
this is the code..I just tried to draw a rectangle first than to deal with mouse thing but I was not successful at drawing.
However, I have noticed that the rectangle is actually existing but it is under the WebBrowser object. I need my rectangle to be on top of the WebBrowser.
How can I bring the Rectangle on to the top of my WebBrowser so it can be visible?
Change
Graphics g = webBrowser1.CreateGraphics();
with
Graphics g = Form1.CreateGraphics();
Assuming Form1 is the name of your form.
Related
I have a custom control that displays an image. I'm using a custom control because I've implemented zooming functionality and scrollbars.
I'm draw a rectangle on the image by doing:
if (drawRect)
{
var g = Graphics.FromImage(Image);
g.DrawRectangle(Pens.Red, rect);
}
The drawing of the rectangle is toggled by a button. However, when I toggle off, the rectangle it's still being drawn on the image.
I'm assuming that once something is drawn on the image's surface, it stays there.
Is there a way to "undraw" something or do I need to implement a second "transparent" image that will act as an overlay?
I am creating a application which displays a live view from a uEye camera in c# using visual studio. I have displayed the camera view in a picturebox.
I am wanting the user to type in a width and height they would like, once they have done this it will display this rectangle on the picturebox which has the camera view in.
I am using the following code...
Pen p = new Pen(Color.Red, 1);
Rectangle rect = new Rectangle(10, 10, Convert.ToInt32(width), Convert.ToInt32(height));
DisplayWindow.CreateGraphics().DrawRectangle(p, rect);
This draws the rectangle on the picture box but for only a second, then it disappears. Anyone know why this is?
Thanks guys
You will need to implement something like this; what happens is async drawing, resulting in strange behaviour.
DisplayWindow.Paint += (o, e) =>
{
e.Graphics.DrawRectangle(p, rect);
};
//To force redraw, just call
DisplayWindow.Invalidate();
I have to draw some text on pictureBox image (Gray scale image). I got some codes. its working but its moving with the image while panning and zooming, and it disappear while changing the window level.
rect = pictureBox1.ClientRectangle;
Graphics g = Graphics.FromImage(bmp);
SolidBrush brush = new SolidBrush(Color.Green);
Font f = new Font("Arial", 15);
g.DrawString("Murugesan", f, brush, start);
I want the text in the permanent location and it never disappear while changing the window level. Anybody there to help me.
You should draw it on PictureBox OnPaint event and use e.Graphics.
Probably you wouldn't have artifacts you mentioned if you used:
Graphics g = pictureBox.CreateGraphics();
But paint on event is still better than that.
Hey everyone, a new guy here in C#.Net.
I'm trying to make an application like Ms Paint, of course much simpler, and I'm stuck.
The problem is this.
In pictureBox, I'm drawing grid lines on the PictureBox, after that I'm reading a .map(A Mapper3 file) and want to draw onto grid lines, but When I draw the map, The grid lines disappers.
I think the problem is because of the PictureBox Image becomes null while I'm drawing the map. How can I overcome this, is there any tricks?
Thanks for the replies from now on, and sorry for my bad English...
My best Regards...
Do you using winforms? If yes, you actually dont need picture box for working area. I think more appropriate would be Graphics class on form or panel. You have lost lines because of form repaint circle, put your drawing code into form paint handler and picture would be repainted when it needed. In some cases you can need to manual trigger repaint circle, for this purposes you should use Invalidate method of your form.
For example, add this code to paint handler:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Drawing vertical lines
for (int x = 5; x < this.ClientRectangle.Width; x+=5)
{
e.Graphics.DrawLine(Pens.Gray, new Point(x, 0), new Point(x, this.ClientRectangle.Height));
}
// Drawing horisontal lines
for (int y = 5; y < this.ClientRectangle.Width; y += 5)
{
e.Graphics.DrawLine(Pens.Gray, new Point(0, y), new Point(this.ClientRectangle.Width,y));
}
}
You also may use Graphics in button click handler this way:
Graphics g = Graphics.FromHwnd(this.Handle);
g.FillEllipse(Brushes.Beige, new Rectangle(10, 10, 10, 10));
But in this case all you have drawn would be erased during form's repaint circle and you will have to repeint it in form paint handler
[EDIT]
Ok, for example you have pictureBox1 on your form, you can easly draw into it with help of Bitmap class in this way:
// Draw into bitmap
Bitmap bmp = new Bitmap(150, 150);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Green, new Rectangle(25, 75, 10, 30));
// Set bitmap into picture box
pictureBox1.Image = bmp;
In this case you have no need to redraw your paintings, picture box would do it for you. Dont forget to set BackColor ot picture box to Transparent if you prefer to show paintings from below of picture box.
You have to draw everything including the grid lines whenever the paint event raised, if you are concerned about performance you may detect the clipping area and only draw that portion.
Good luck.
I have a rectangle which i am drawing on a form. The rectangle is coded.I want to resize that rectangle according to the resizing of the form, that is as and when the form is resized, the rectangle is resized accordinly. How can I do it?
Do i need to make changes in the way i am drawing the rectangle which by the way right now is like this : Rectangle rect = new Rectangle(0, 0, 650, 50);
I used the Resize events in which i assigned the new size by this I did new Size() but that is making my rectangle disappear from the form.
Add the following in the OnPaint handler of your form:
rect.Width = this.Width;
rect.Height = this.Height;
That should keep the form filled with your rectangle.
The OnPaint event should look something like this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
rect.Width = this.Width;
rect.Height = this.Height;
}
This would resize your rectangle everytime the form is repainted, which includes resizing as well.
You should do all the drawing of your form at the override of the OnPaint method. You need to check the size of the form each time you draw the rectangle if you want to make the size of the rectangle relative to the size of the Form.
You shouldn't do anything with the Resize event (maybe just cache the size needed of the rectangle to avoid having to calculate that size each time the OnPaint is called).
To do that, you'll have to create a resizing function to your rectangle in the Form Resize event of your form. And to keep the rectangle proportions relative to the form size you'll have to code some simple math.