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);
}
Related
I'm trying to call a method which draws something in a panel from another class, but something doesn't work.
This is the drawing method:
class Dibujo
{
public void Dibujar(Object sender, PaintEventArgs e)
{
Form2 ventana2 = new Form2();
Graphics g = ventana2.panel1.CreateGraphics();
Pen Boli = new Pen(Brushes.Green);
Point Inicio = new Point(200, 100);
Point Final = new Point(400, 300);
g.DrawLine(Boli, Inicio, Final);
}
}
}
And here is were I call it:
private void btnDibujar_Click(object sender, EventArgs e){
Graphics g = panel1.CreateGraphics();
Rectangle rect = new Rectangle();
PaintEventArgs pe = new PaintEventArgs(g,rect);
Pen Boli = new Pen(Brushes.Red);
g.DrawLine(Boli,100,100,100,300);
Dibujo panel = new Dibujo();
panel.Dibujar(sender,pe);
}
So the idea is that when I click the button btnDibujar_click a simple line is drawn in the panel but it doesn't work. I know that the code probably has a lot of mistakes but I'm a beginner and I'm learning by myself so all kinds of advice is welcome.
I am creating a Graphics Form where objects with coordinates x,y are being drawn into the Graphics. It works properly for small x and y, but when I want to draw them in different place (f.e. x = 500, y = 300) they disappear.
public WindowHandler()
{
dc = this.CreateGraphics();
this.Size = new Size(sizeX, sizeY); // 800x600
startSimulation = new Button
{
// button properties
};
this.Controls.Add(startSimulation);
startSimulation.Click += new EventHandler(StartSimulationClick);
}
private void CreationsMethods()
{
creations.PaintAllAnimals(dc);
}
public void PaintAllAnimals(Graphics g)
{
foreach (var animal in ecoStructure.world.animals)
{
animal.PaintAnimal(g);
}
}
public void PaintAnimal(Graphics graphics)
{
Rectangle rectangle = new Rectangle(x, y, 3, 3);
Pen pen = new Pen(colour);
graphics.DrawRectangle(pen, rectangle);
graphics.FillRectangle(colour, rectangle);
}
I want to put all the objects onto the window. Is there any way to make the Graphics "bigger"? Do I need to make another one? Or should I use different tool to draw rectangles?
Thanks to #Chris Dunaway for posting an answer in comment.
So I deleted the CreateCraphics, and instead of that i am now using an OnPaint method. It works slowly, but works. So I will try to make it as fast as i can. For now, I just created this. NextStepClick is how I use the OnPaint to paint the rectangles.
private void CreationsMethods(object sender, PaintEventArgs e)
{
dc = e.Graphics;
base.OnPaint(e);
creations.PaintAllAnimals(dc);
}
private void NextStepClick(object sender, EventArgs e)
{
this.Refresh();
picBox.Paint += new System.Windows.Forms.PaintEventHandler(CreationsMethods);
}
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 to ZOOM-IN & ZOOM-OUT specific portion of 2D-graphics like (line, Rectangle, circle) which i have drawn on winform.
I am not used any picture box, panel.
I crated simple program to draw circle & on button click try to zoom
but it showing error "parameter is not valid"
in method Drawing() # Line- DeviceContexct.Transform = mainViewTransform;
public Graphics DeviceContexct;
public Matrix mainViewTransform = new Matrix();
private void ScalingCircle_Paint( object sender, PaintEventArgs e )
{
Pen myPen = new Pen(Color.Blue, 1);
e.Graphics.DrawRectangle(myPen, 50, 50, 100, 100);
mainViewTransform.Scale(3, 2);
DeviceContexct = e.Graphics;
}
private void Drawing(Graphics gr)
{
Pen myPen2 = new Pen(Color.Red, 1);
DeviceContexct.Transform = mainViewTransform;
DeviceContexct.DrawRectangle(myPen2, 50, 50, 100, 100);
}
private void button1_Click( object sender, EventArgs e )
{
Drawing(DeviceContexct);
}
I prefer to draw into Bitmaps with System.Drawing.Graphics object.
Then you can use your graphics object with "go.DrawImage(...)" to draw the bitmap directly into your winforms and actually give it a scale.
https://msdn.microsoft.com/en-us//library/ms142040(v=vs.110).aspx
I got the solution, thanks for your help.
I only need to call refresh/invalidate etc on button click.
public partial class ScalingCircle : Form
{
public Graphics DeviceContexct;
// current transformation matrix of main view (offset & scaling)
public Matrix mainViewTransform = new Matrix();
public int scale = 1;
public ScalingCircle()
{
InitializeComponent();
DeviceContexct = Graphics.FromHwnd(this.Handle);
DeviceContexct = this.CreateGraphics();
}
public void ScalingCircle_Paint(object sender, PaintEventArgs e)
{
DeviceContexct = e.Graphics;
DeviceContexct.PageUnit = GraphicsUnit.Pixel;
DeviceContexct.Transform = mainViewTransform;
ScalingCircle1(scale);
}
private void ScalingCircle1(int x )
{
Pen myPen2 = new Pen(Color.Black, 1);
DeviceContexct.Transform = mainViewTransform;
Rectangle myRectangle = new Rectangle(50, 50, 100 * x, 100 * x);
DeviceContexct.FillRectangle(new SolidBrush(Color.BurlyWood), myRectangle);
}
private void ScalingCircle_Load( object sender, EventArgs e )
{
this.ResizeRedraw = true;
}
private void button1_Click( object sender, EventArgs e )
{
scale += 5;
this.Refresh();
}
private void button2_Click( object sender, EventArgs e )
{
if (scale > 1)
{
scale -= 5;
this.Refresh();
}
}
}
You can use transformations for that. The graphics object you use for painting things has a Transformation property of type System.Drawing.Drawing2D.Matrix.
Graphics also has the ScaleTransform method.
I haven't used it myself, but that is how microsofts Chart does it.
https://msdn.microsoft.com/de-de/library/system.drawing.drawing2d.matrix(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/system.drawing.graphics.scaletransform(v=vs.110).aspx
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);
}