C# How to Move A Circle Along a Rectangle Path - c#

I need to move a point along a rectangle path at the command of a button. I want it to start at the upper right corner of the rectangle path, but I am not sure how to get it to go all the way around the path and stop at the original point. The screen refreshes at the speed provided by the user in an input box. Thank you in advance!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace Assignment_2
{
public partial class Form1 : Form
{
private const int formwidth = 1280;
private const int formheight = 720;
private const int ball_a_radius = 10;
private const int horizontaladjustment = 8;
private const double ball_a_distance_moved_per_refresh = 1.6;
private double ball_a_real_coord_x = 515;
private double ball_a_real_coord_y = 40;
private int ball_a_int_coord_x;
private int ball_a_int_coord_y;
private const double graphicrefreshrate = 30.0;
private static System.Timers.Timer graphic_area_refresh_clock = new System.Timers.Timer();
private static System.Timers.Timer ball_a_control_clock = new System.Timers.Timer();
private bool ball_a_clock_active = false;
public double speed = 0;
public Form1()
{
InitializeComponent();
ball_a_int_coord_x = (int)(ball_a_real_coord_x);
ball_a_int_coord_y = (int)(ball_a_real_coord_y);
System.Console.WriteLine("Initial coordinates: ball_a_int_coord_x = {0}. ball_a_int_coord_y = {1}.",
ball_a_int_coord_x, ball_a_int_coord_y);
graphic_area_refresh_clock.Enabled = false;
graphic_area_refresh_clock.Elapsed += new ElapsedEventHandler(Updatedisplay);
ball_a_control_clock.Enabled = false;
ball_a_control_clock.Elapsed += new ElapsedEventHandler(Updateballa);
Startgraphicclock(graphicrefreshrate);
Startballaclock(speed);
}
public class NumericTextBox : TextBox
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void panel2_Paint_1(object sender, PaintEventArgs e)
{
//Create pen
Pen blackPen = new Pen(Color.Black, 1);
//Create rectangle
Rectangle rect = new Rectangle(125, 50, 400, 400);
//Draw rectangle to screen
e.Graphics.DrawRectangle(blackPen, rect);
Graphics graph = e.Graphics;
graph.FillEllipse(Brushes.Green, ball_a_int_coord_x, ball_a_int_coord_y, 2 * ball_a_radius, 2 * ball_a_radius);
base.OnPaint(e);
}
public void button7_Click(object sender, EventArgs e)
{
speed = Convert.ToInt32(textBox3.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
protected void Startgraphicclock(double refreshrate)
{
double elapsedtimebetweentics;
if (refreshrate < 1.0) refreshrate = 1.0;
elapsedtimebetweentics = 1000.0 / refreshrate;
graphic_area_refresh_clock.Interval = (int)System.Math.Round(elapsedtimebetweentics);
graphic_area_refresh_clock.Enabled = true;
}
protected void Startballaclock(double updaterate)
{
double elapsedtimebetweenballmoves;
if (updaterate < 1.0) updaterate = 1.0;
elapsedtimebetweenballmoves = 1000.0 / updaterate;
ball_a_control_clock.Interval = (int)System.Math.Round(elapsedtimebetweenballmoves);
ball_a_control_clock.Enabled = true;
ball_a_clock_active = true;
}
protected void Updatedisplay(System.Object sender, ElapsedEventArgs evt)
{
Invalidate();
if (!(ball_a_clock_active))
{
graphic_area_refresh_clock.Enabled = false;
System.Console.WriteLine("The graphical area is no longer refreshing. You may close the window.");
}
}
protected void Updateballa(System.Object sender, ElapsedEventArgs evt)
{
ball_a_real_coord_x = ball_a_real_coord_x - 5;
ball_a_real_coord_y = ball_a_real_coord_y - 5;
ball_a_int_coord_x = (int)System.Math.Round(ball_a_real_coord_x);
ball_a_int_coord_y = (int)System.Math.Round(ball_a_real_coord_y);
if (ball_a_int_coord_x >= formwidth || ball_a_int_coord_y + 2 * ball_a_radius <= 0 || ball_a_int_coord_y >= formheight)
{
ball_a_clock_active = false;
ball_a_control_clock.Enabled = false;
System.Console.WriteLine("The clock controlling ball a has stopped.");
}
}
private void button4_Click(object sender, EventArgs e)
{
ball_a_control_clock.Enabled = true;
}
}
}

I have a more straightforward way to move a circle. Your code is too long for me to read. See if you like this!
If I were you, I would use a PictureBox. I first create an image of a circle, and then put that image in the PictureBox. Then you can just use a timer to change the position of the PictureBox.
You should set the Interval of the timer to 33 ms, which is roughly 30 fps. This is how you would programme the timer:
Keep a counter to indicate how many pixels the circle has moved. Let's say you want it to move in a 100px x 50px rectangular path.
For every 33ms,
If the counter is less than 100,
increase the X position and the counter by 1,
if the counter is between 101 and 150,
increase the Y position and the counter by 1,
if the counter is between 151 and 250,
decrease the X position by 1 and increment the counter
if the counter is between 251 and 300,
decrease the Y position by 1 and increment the counter
if the counter is greater than 300,
stop the timer
I really don't like drawing stuff on the screen with the OnPaint event. I mean, you are moving a ball! People think of this as changing the x and y positions of a ball, not as deleting the ball at the previous position and drawing it in the new position. Changing the position of the picture box just makes LOTS more sense, don't you think so?

Related

(C# - Forms) How do i get user input from TextBox.Text?

I'm trying to write a program that paints a polygon onto a PictureBox. I want the user to enter values such as the center's X and Y point, length, angle, number of edges to textboxes. Then I want to use the values in textboxes as parameters.
The problem is that the program throws different types of exceptions as soon as I launch it without letting me enter any values into the textboxes. I guess it takes the TextBox.Text as a null value or an empty string so other parts of my code fail.
How do I get TextBox.Text?
Also, if you have any suggestions about calculating the vertex points of a polygon please share it with me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleApp1;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public int CenterX, CenterY, Length, Angle;
public int Edges;
private void button1_Click(object sender, EventArgs e)
{
CenterX = int.Parse(textBox1.Text);
CenterY = int.Parse(textBox2.Text);
Length = int.Parse(textBox3.Text);
Angle = int.Parse(textBox4.Text);
Edges = int.Parse(textBox5.Text);
pictureBox1.Invalidate();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
/////////////////////////////////////////////////////////////////////////////
pictureBox1.CreateGraphics();
/////////////////////////////////////////////////////////////////////////////
int width = pictureBox1.ClientSize.Width;
int height = pictureBox1.ClientSize.Height;
int newWidth = width / 2;
int newHeight = height / 2;
e.Graphics.TranslateTransform((float)newWidth, (float)newHeight);
/////////////////////////////////////////////////////////////////////////////
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
/////////////////////////////////////////////////////////////////////////////
Pen pen = new Pen(Color.Black, 5);
/////////////////////////////////////////////////////////////////////////////
Polygon polygon = new Polygon(CenterX, CenterY);
polygon.LENGTH = Length;
polygon.ROTATIONANGLE = Angle;
polygon.NUMBER_OF_EDGES = Edges;
polygon.calculateEdgeCoordinates();
polygon.rotatePolygon();
/////////////////////////////////////////////////////////////////////////////
PointF[] points = new PointF[polygon.rotatedPoints.Count];
for (int i = 0; i < polygon.rotatedPoints.Count; i++)
{
points[i] = polygon.rotatedPoints[i];
}
e.Graphics.DrawPolygon(pen, points);
e.Dispose();
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
public class Polygon
{
Point2D center = new Point2D();
public List<Point2D> edgePoints = new List<Point2D>();
public List<PointF> rotatedPoints = new List<PointF>();
private double radius, angle, length, rotationAngle; private int numberOfEdges;
public double RADIUS { get => radius; set => radius = value; }
public double LENGTH { get => length; set => length = value; }
public double ROTATIONANGLE { get => rotationAngle; set => rotationAngle = value; }
public int NUMBER_OF_EDGES { get => numberOfEdges; set => numberOfEdges = value; }
public Polygon()
{
Point2D polarCoords = center.calculatePolarCoordinates();
polarCoords.X = length;
angle = polarCoords.Y;
}
public Polygon(double x, double y)
{
center.X = x;
center.Y = y;
Point2D polarCoords = center.calculatePolarCoordinates();
polarCoords.X = length;
angle = polarCoords.Y;
}
public void calculateEdgeCoordinates()
{
double interiorAngle = 360 / numberOfEdges;
for (int i=0; i < numberOfEdges; i++)
{
if (i == 0)
{
Point2D point = new Point2D(length, angle);
edgePoints.Add(point);
}
else
{
Point2D point = new Point2D(length, angle+interiorAngle);
edgePoints.Add(point);
}
}
}
public void rotatePolygon()
{
for (int i=0; i < edgePoints.Count; i++)
{
edgePoints[i].Y += rotationAngle;
edgePoints[i].calculateCartesianCoordinates();
PointF point = new PointF((float)(edgePoints[i].X), (float)(edgePoints[i].Y));
rotatedPoints.Add(point);
}
}
}
}
This is necessary to start off with
private void button1_Click(object sender, EventArgs e)
{
if (!int.TryParse(textBox1.Text, out CenterX))
CenterX = 0;
if (!int.TryParse(textBox2.Text, out CenterY))
CenterY = 0;
if (!int.TryParse(textBox3.Text, out Length))
Length = 0;
if (!int.TryParse(textBox4.Text, out Angle))
Angle = 0;
if (!int.TryParse(textBox5.Text, out Edges))
Edges = 0;
pictureBox1.Invalidate();
}
Then also use this: (here's your 0 division if numberOfEdges = 0)
double interiorAngle = 360;
if (numberOfEdges != 0)
interiorAngle = 360 / numberOfEdges;

Image is flashing when updated new position

I have 2 picture objects.
I want both of them to move from right to left.
If one go out of the visible panel, I replace its position to the start point.
So there are always 2 pictures moving on the screen.
If I don't use timer, 2 pictures are painted. But if I use a timer with tick event updating their positions to make them move, there is only 1 picture is shown and it's keep flashing, lagging...
Below is my code so far. I'm not familiar with C#. Appreciate any help. Thank you.
Timer interval = 30;
Form 1:
public partial class Form1 : Form
{
Background bg1 = new Background();
Background bg2 = new Background(800);
public Form1()
{
InitializeComponent();
}
private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
bg1.paint(e);
bg2.paint(e);
}
private void Timer_Tick(object sender, EventArgs e)
{
bg1.updatePosition();
bg2.updatePosition();
this.Refresh();
}
}
Background:
class Background
{
int bg_width = 800;
int bg_height = 500;
Image bg;
Rectangle wb;
private static int x = 0;
public Background()
{
bg = Properties.Resources.bg;
wb = new Rectangle(x, 0, bg_width, bg_height);
}
public Background(int custom_x)
{
x = custom_x;
bg = Properties.Resources.bg;
wb = new Rectangle(x, 0, bg_width, bg_height);
}
public void paint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bg, wb);
}
public void updatePosition()
{
x--;
if (x == -800)
{
x = 801;
}
wb.Location = new Point(x, 0);
}
}

Winforms Graphics flickering. (Double buffering doesn't help!)

I'm trying to create a simple Windows Forms graphics app that basically will draw a circle every time the user clicks and it expands, while slowly fading away.
When I tried to use the Paint() Event for my graphics functionality, nothing happened, so I created a separate function called "Render" that is called in my main update Timer.
The app worked but the graphics flickered. After some researched I realized that I had to enable Double Buffering so that it would render to a buffer and then the buffer would be rendered to the screen.
The flickering still didn't stop!
Is this because double buffering only works for Paint() events and if so how do I get the Paint() event to work or am I not enabling Double Buffering right?
Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Widget
{
public class Circle
{
public float X;
public float Y;
public float Radius;
public int Alpha;
public Circle(float X, float Y, float Radius, int Alpha)
{
this.X = X;
this.Y = Y;
this.Radius = Radius;
this.Alpha = Alpha;
}
}
public partial class Form1 : Form
{
public static readonly int ScreenX = Screen.PrimaryScreen.Bounds.Width;
public static readonly int ScreenY = Screen.PrimaryScreen.Bounds.Height;
public int WindowWidth = 500, WindowHeight = 500;
public Graphics G;
private Pen Pen;
private Timer timer = new Timer();
private List<Circle> Circles;
public Form1()
{
this.Text = "Widget - Sam Brandt";
this.Size = new Size(WindowWidth, WindowHeight);
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(ScreenX - WindowWidth - 100, 0);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Icon = new Icon("C:\\Users\\Admin\\Desktop\\Code Repositories\\Visual Studios\\Widget\\Widget\\Properties\\WidgetIcon.ico");
Pen = new Pen(Color.Black, 1);
G = CreateGraphics();
//this.Paint += new PaintEventHandler(OnPaint);
ConstructMouse();
FormWithTimer();
DoubleBuffered = true;
Circles = new List<Circle>();
}
public void ConstructMouse()
{
this.MouseUp += new MouseEventHandler(OnMouseUp);
this.MouseMove += new MouseEventHandler(OnMouseMove);
this.MouseDown += new MouseEventHandler(OnMouseDown);
}
public void FormWithTimer()
{
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (10);
timer.Enabled = true;
timer.Start();
}
protected void OnMouseUp(object sender, MouseEventArgs e)
{
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
}
public void OnMouseDown(object sender, MouseEventArgs e)
{
Circles.Add(new Circle(e.Location.X, e.Location.Y, 0, 255));
}
/*public void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
for (int i = 0; i < Circles.Count; i++)
{
Circle C = Circles[i];
e.Graphics.DrawEllipse(new Pen(Color.FromArgb(C.Alpha, 0, 0, 0), 1), C.X - C.Radius, C.Y - C.Radius, 2 * C.Radius, 2 * C.Radius);
}
}*/
private void Tick()
{
for (int i = 0; i < Circles.Count; i++)
{
Circle C = Circles[i];
C.Radius++;
C.Alpha -= 3;
if (C.Alpha == 0)
{
Circles.RemoveAt(i);
}
}
}
public void Render()
{
G.Clear(Color.White);
for (int i = 0; i < Circles.Count; i++)
{
Circle C = Circles[i];
G.DrawEllipse(new Pen(Color.FromArgb(C.Alpha, 0, 0, 0), 1), C.X - C.Radius, C.Y - C.Radius, 2 * C.Radius, 2 * C.Radius);
}
}
public void timer_Tick(object sender, EventArgs e)
{
Render();
Tick();
}
}
}
Short answer - keep DoubleBuffered = true and use Paint event.
When I tried to use a PaintEvent for my graphics functionality, nothing happened
When you do some modifications and want to reflect them, use Control.Invalidate method, which according to the documentation
Invalidates the entire surface of the control and causes the control to be redrawn.
In your case, something like this
void timer_Tick(object sender, EventArgs e)
{
Tick();
Invalidate();
}
More observations here but probably will be the answer.
Why Timer?
Use the Paint event, it is called when the GDI+ determines it needs to, you are constantly painting with your code as-is.
Your code makes it look like you are not using double buffering.
I'd do all your drawing to a separate Graphics object, and then copy that to your main Graphics object on the timer tick event only if there's been a change. You may need to keep track of that with a boolean member. This means your background drawing will have to be triggered by some other event.
If your picture is actually changing every 10 milliseconds, I'd slow down the timer a bit and set it to 50 milliseconds.
Try this (in VB):
Dim aProp = GetType(Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
aProp.SetValue(Me, True, Nothing)
Also had problems with double buffering, and usual setting property DoubleBuffered to true do not work. I have found this solution somewhere on web

how can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?

How can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?
namespace Ball_timer_2005
{
public class Form1 : System.Windows.Forms.Form
{
const int radius = 20;
const int velocity = 5;
int xC, yC, xDelta=10, yDelta=10, xSize, ySize; // class level variables
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
// TODO: Add any constructor code after InitializeComponent call
this.ResizeRedraw = true; // Tell form to redraw itself when resized
timer1.Start();
Form1_Resize(this, EventArgs.Empty); // Force a Resize Event as pgm starts
//
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 25;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Bouncing Ball";
this.Resize += new System.EventHandler(this.Form1_Resize);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Resize(object sender, System.EventArgs e)
{
xSize = this.ClientSize.Width; // Set current window size
ySize = this.ClientSize.Height;
xC = xSize/2; // Place ball in center of window
yC = ySize/2;
DrawBall(); // Draw the ball in the window
}
private void timer1_Tick(object sender, System.EventArgs e)
{
DrawBall(); // Draw ball in next frame of animation
}
private void DrawBall()
{
Graphics g = this.CreateGraphics();
Brush b = new SolidBrush(this.BackColor);
g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius); //erase old ball
xC += xDelta; //move ball
yC += yDelta;
if ((xC+radius >= ClientSize.Width) || (xC - radius <= 0)) //check for wall hits
xDelta = -xDelta;
if ((yC+radius >= ClientSize.Height) || (yC - radius <= 0))
yDelta = -yDelta;
b = new SolidBrush(Color.GreenYellow); // draw new ball
g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius);
b.Dispose();
g.Dispose();
}
}
}
1.this is the code i have so far can some one help ???
1.this is the code i have so far can some one help ???
1.this is the code i have so far can some one help ???
You can do this by Handling MouseClick event of the Form.
Step1: Store the Ellipse Co-ordinates into the Region to identify the position of the Mouse Click later.
//Declare it as class members
List<Region> regionList = new List<Region>();
int EclipseX = 10;
int EclipseY = 50;
int BallWidth = 100;
int BallHeight = 100;
//inside paint function
SolidBrush brush = new SolidBrush(Color.Blue);
e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));
Step2: now declare the MouseClick event of the Form
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
Step3: now handle the MouseClick event of the Form.
the MouseClick event provides MouseEventArgs parameter here e.
this MouseEventArgs has two properties.
first property is X gives you the X-axis Location of the
Mouse Click.
second property is Y gives you the Y-axis location of the Mouse Click.
you need to compare these X and Y values with existing Region values using IsVisible Property.
you need to get the each Region from the List (which was added while drawing
Ellipse) and use IsVisible Property of the Region class to
detect whether mouse clicked on the Ellipse or not.
//declare it as class member
int MouseClicksCount = 0;
bool isfound=false;
//event handler
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
isfound=false;
foreach(Region r in regionList)
{
if (r.IsVisible(e.X, e.Y))
{
isfound=true;
break;
}
}
if (isfound)
MouseClicksCount++;
}
Complete Solution(Sample code):
public partial class Form1 : Form
{
int MouseClicksCount = 0;
List<Region> regionList = new List<Region>();
int EclipseX = 10;
int EclipseY = 50;
int BallWidth = 100;
int BallHeight = 100;
bool isfound=false;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
isfound=false;
foreach(Region r in regionList)
{
if (r.IsVisible(e.X, e.Y))
{
isfound=true;
break;
}
}
if (isfound)
MouseClicksCount++;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Blue);
e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));
}
}
Does this help at all
http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-form
You may be able to us this to detect an overlap of mouse position/ ball width radius

Double buffering with C# has negative effect

I have written the following simple program, which draws lines on the screen every 100 milliseconds (triggered by timer1). I noticed that the drawing flickers a bit (that is, the window is not always completely blue, but some gray shines through). So my idea was to use double-buffering. But when I did that, it made things even worse. Now the screen was almost always gray, and only occasionally did the blue color come through (demonstrated by timer2, switching the DoubleBuffered property every 2000 milliseconds).
What could be an explanation for this?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Blue, 1.0f);
Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
// every 100 ms
private void timer1_Tick(object sender, EventArgs e) {
Invalidate();
}
// every 2000 ms
private void timer2_Tick(object sender, EventArgs e) {
DoubleBuffered = !DoubleBuffered;
this.Text = DoubleBuffered ? "yes" : "no";
}
}
}
I would just draw all of your items to your own buffer, then copy it all in at once. I've used this for graphics in many applications, and it has always worked very well for me:
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();// every 100 ms
}
private void Form1_Load(object sender, EventArgs e)
{
DoubleBuffered = true;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap buffer = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(buffer);
Pen pen = new Pen(Color.Blue, 1.0f);
//Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
BackgroundImage = buffer;
}
EDIT: After further investigation, it looks like your problem is what you're setting your Graphics object to:
Graphics g = CreateGraphics();
needs to be:
Graphics g = e.Graphics();
So your problem can be solved by either creating a manual buffer like I did above, or simply changing you Graphics object. I've tested both and they both work.
No need to use multiple buffers or Bitmap objects or anything.
Why don't you use the Graphics object provided by the Paint event? Like this:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 1.0f);
Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
Try setting the double buffered property to true just once in the constructor while you're testing.
You need to make use of the back buffer. Try this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DoubleBufferTest
{
public partial class Form1 : Form
{
private BufferedGraphicsContext context;
private BufferedGraphics grafx;
public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(this.OnResize);
DoubleBuffered = true;
// Retrieves the BufferedGraphicsContext for the
// current application domain.
context = BufferedGraphicsManager.Current;
UpdateBuffer();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Refresh();
}
private void OnResize(object sender, EventArgs e)
{
UpdateBuffer();
this.Refresh();
}
private void UpdateBuffer()
{
// Sets the maximum size for the primary graphics buffer
// of the buffered graphics context for the application
// domain. Any allocation requests for a buffer larger
// than this will create a temporary buffered graphics
// context to host the graphics buffer.
context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
// Allocates a graphics buffer the size of this form
// using the pixel format of the Graphics created by
// the Form.CreateGraphics() method, which returns a
// Graphics object that matches the pixel format of the form.
grafx = context.Allocate(this.CreateGraphics(),
new Rectangle(0, 0, this.Width, this.Height));
// Draw the first frame to the buffer.
DrawToBuffer(grafx.Graphics);
}
protected override void OnPaint(PaintEventArgs e)
{
grafx.Render(e.Graphics);
}
private void DrawToBuffer(Graphics g)
{
//Graphics g = grafx.Graphics;
Pen pen = new Pen(Color.Blue, 1.0f);
//Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
}
}
It's a slightly hacked around version of a double buffering example on MSDN.

Categories