Im trying to draw a graphic on a form upon form startup. I've tried putting the graphic components inside the form constructer, but i cannot seem to get it working. This is what i've got so far.
public partial class Form3 : Form
{
public Form3()
{
System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}
}
Any and all help will be awesome,
Thanks guys
One solution would be to use a Bitmap, e.g.
Bitmap b = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(b);
... // Draw
this.BackgroundImage = b;
Otherwise, you'll need to handle the Paint event, whereat you can draw directly to the graphics context each time the form is invalidated. e.g.
this.Paint += Form1_Paint;
...
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
g.DrawEllipse(System.Drawing.Pens.Black, rectangle);
g.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
e.Graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
e.Graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}
Related
I have a Form with a PictureBox (or Panel) and I want to draw a rectangle in the point (0,0) of this PictureBox.
How Can i do??
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DrawIt()
{
System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(50, 50, 150, 150);
SolidBrush brush = new SolidBrush(Color.Red);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
graphics.FillRectangle(brush, rectangle);
}
private void button1_Click(object sender, EventArgs e)
{
this.DrawIt();
}
}`
private void button1_Click(object sender, EventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Red);
Pen pen = new Pen(Color.Red);
Rectangle rectangle = new Rectangle(50, 50, 150, 150);
pictureBox1.CreateGraphics().DrawReactangle(pen, rectangle);
pictureBox1.CreateGraphics().FillRectangle(brush, rectangle);
}
Trying to draw a shape over existing panels for a good while, but by now out of ideas. Could somebody help me out, please? It ends up always behind the panels (and pictureBox /the grey one/). I tried 3 different ways, whithout success. this is my code:
namespace DrawingOnFront
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel11_MouseClick(object sender, MouseEventArgs e)
{
DrawIt(90, 70);
}
private void DrawIt(int x, int y)
{
Rectangle Circle = new Rectangle(x,y,40,40);
SolidBrush Red = new SolidBrush(Color.Red);
Graphics g = this.CreateGraphics();
g.FillEllipse(Red, Circle);
/*
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int width = pictureBox1.Width /4;
int height = pictureBox1.Height /2;
int diameter = Math.Min(width, height);
g.FillEllipse(Red, x, y, width, height);
pictureBox1.Image = bmp;
*/
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Graphics g = e.Graphics)
{
Rectangle Circle = ClientRectangle;
Circle.Location = new Point(100, 60);
Circle.Size = new Size(40, 40);
using (SolidBrush Green = new SolidBrush(Color.Green))
{
g.FillEllipse(Green, Circle);
}
}
}
}
}
Sorry for this basic lama question, probably for most of you it is very easy, I am still learning it. Thanks a lot in advance.
My comments above apply. Here is an example of how to draw onto each control and the form separately:
We best have a common drawing routine that we can call from the Paint event of each participating element, in our case a Panel, a PictureBox and the Form.
The trick is for all nested elements to draw the circle shifted by their own location. To do so we pass these things into the drawing routine:
a valid Graphics object. We get it from the Paint events.
and a reference to the control; we use it to offset the drawing on each control (except the form) with Graphics.TranslateTransform..:
Result:
As you can see it looks as if we painted one circle over all elements but actually we drew three circles, each onto one element..:
private void canvasForm_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
draw(sender as Control, e.Graphics);
}
private void draw(Control ctl, Graphics g)
{
Rectangle r = new Rectangle(200, 100, 75, 75);
if (ctl != canvasForm) g.TranslateTransform(-ctl.Left, -ctl.Top);
g.FillEllipse(Brushes.Green, r);
g.ResetTransform();
}
Note that the same result could be created with three calls, one FillRectangle, one DrawImage and one FillEllipse :-)
I have a rectangle drawn in the background of my app window with this code :
public Form1()
{
InitializeComponent();
Paint += myDrawingPanel_Paint;
}
private void myDrawingPanel_Paint(object sender, PaintEventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 102, 205));
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 100, Form1.ActiveForm.Width, Form1.ActiveForm.Height));
myBrush.Dispose();
formGraphics.Dispose();
}
The buttons on this rectangle all have this white border around them and I'd like to remove them.
I tried to set the margins to 0, played with the appearance options but never managed to get rid of them.
I don't know what to pass to my method to call my Circle drawing properly.
Here is my code:
private void Circle()
{
Graphics g1 = e.Graphics;
Pen p1 = new Pen(Color.Black);
g1.DrawEllipse(p1, 12, 12, 50, 50);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Circle();
}
This is not working , cause there is no 'e'.
If i rewrite my code from
Graphics g1 = e.Graphics;
to
Graphics g1 = this.CreateGraphics();
It will draw circle but not in the picturebox. I need a circle to be inside picturebox.
If your method requires a reference to the PaintEventArgs, then why not supply it one? Something like this:
private void Circle(PaintEventArgs e)
{
Graphics g1 = e.Graphics;
Pen p1 = new Pen(Color.Black);
g1.DrawEllipse(p1, 12, 12, 50, 50);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Circle(e);
}
This would allow other paint event handlers to make use of that method for their own controls as well.
To get a reference to a Graphics object you can provide that via the parameters. Like
private void Circle(Graphics g)
{
Pen p1 = new Pen(Color.Black);
g.DrawEllipse(p1, 12, 12, 50, 50);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics myg = e.Graphics;
Circle(myg);
}
I want to save image from panel to bitmap and then I want to load the saved image after my Form comes out of minimizing mode.
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, panel1.Bounds);
bmp.Save(#"C:\Test");
panel1.BackgroundImage = Image.FromFile(#"C:\Test");
And what event should I use for minimizing event?
P.S. I am a C# beginner.
EDITED
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBitmap.jpeg", ImageFormat.Jpeg);
}
}