How to animate a PictureBox in a Win Forms application? - c#

I have a windows form application with a PictureBox control containing an image. I want to move the PictureBox control to the right in a slow movement. Here is my code:
Point currentPoint = pictureBox_Logo.Location;
for (int i = 0; i < 25; i++)
{
pictureBox_Logo.Location = new Point(pictureBox_Logo.Location.X + 1, pictureBox_Logo.Location.Y);
Thread.Sleep(30);
}
The problem here is that when the code executes instead of seeing the picture move, I see a white picture move and the moving stops until the picture appears.
What am I missing and what can I do about it?

Code:
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}
original thread is here Move images in C#

Try to use pictureBox_Logo.Refresh() after Thread.Sleep(30);
Or look for standard Timer control.

My code is good written, but what I did wrong was putting the code in an event:
private void Form1_Shown(object sender, EventArgs e);
But when I put my code in a button it works without any problems.

Related

Programming Scary Screamer on C#

I am new in c# and trying to develop Scary Screamer Application. It is an joke windows forms application which is running on the PC and invisible in taskbar.
There is timer running in this application. If system datetime.now.minute = 15
It should play scary sound and show scary picture on the screen. After 1-2 seconds picture should disappear from the screen.
But i am stuck and don't know how to make picture disappear. Any Ideas how to do that?
Below is my code:
namespace screamer2
{
public partial class Form1 : Form
{
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Width = Screen.PrimaryScreen.Bounds.Width/2;
this.Height = Screen.PrimaryScreen.Bounds.Height/2;
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
BackgroundImage = Properties.Resources._1;
System.Threading.Thread.Sleep(500);
pla.Play();
}
}
}
}
I would propose to set image once in Form1_Load and then control any showing and hiding of window using Form.Opacity variable. I have tested the code below and should work as you wanted.
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Opacity = 0; //This line added
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height / 2;
this.BackgroundImage = Properties.Resources._1; //We set the image once here
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
this.Opacity = 1; //We show the window
System.Threading.Thread.Sleep(500);
pla.Play();
this.Opacity = 0; //We hide the window
}
}

Moving a picture box in timer C# Winforms

Ok so please keep answers very direct and i must say i am very new to C#, i don't know a lot of stuff. Without further adieu my problem.
I am trying to move a picture box horizontally across the screen on a timer.The timer must go infinitely. I have tried all i currently know in C# and searched around quite a lot but nothing answered my exact question which is what i need because of my lesser knowledge of C#. For the last two weeks i worked on graphics mostly and the rest of that was trying to get this to work, So i have no code in my game. This is because for anything to work i need this part to be working. My game is 2D topdown. Any and all help is appreciated.
Thank you for taking the time to read.
Edit
No more answers needed, Thank you Odrai for the answer, it helped me a lot.
Use pictureBox.Location = new Point(x, y) or set pictureBox.Left/Top/Right. You can define x and y as variabels and initialize them with a default value. Increment x on timer tick.
Sample 1:
public partial class Form1 : Form
{
private Random _random
public Form1()
{
InitializeComponent();
_random = new Random();
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = _random.Next(0, 500);
int y = _random.Next(0, 500);
pictureBox1.Top += y;
pictureBox1.Left += x;
}
}
Sample 2:
private void timer1_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
pictureBox.Location = new Point(picust.Location.X + 10, picust.Location.Y);
this.ResumeLayout();
}
Add two buttons with title LEFT and RIGHT to a form and write the following code.
It might give you an idea, how to do simple moving animations.
public partial class Form1 : Form
{
int difference = 0;
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 15;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
pictureBox1.Left += difference;
}
private void btnLeft_Click(object sender, EventArgs e)
{
difference = -2;
}
private void btnRight_Click(object sender, EventArgs e)
{
difference = 2;
}
}
Try This Code it will work :
private void timer1_Tick(object sender, EventArgs e)
{
int width = this.Width; // get the width of Form.
if(pictureBox1.Location.X > width - pictureBox1.Width) //to check condition if pic box is touch the boundroy of form width
{
pictureBox1.Location = new Point(1, pictureBox1.Location.Y); // pic box is set to the new point. here 1 is indicate of X coordinate.
}
else
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 100, pictureBox1.Location.Y); // to move picture box from x coordinate by 100 Point.
}
}
//Try This //
picturebox1.Location = 0,0;

My variable is not changing as it is supposed to

My purpose is to change drawlines gradient each second in the form application. However it doesn't work. valueble counter " changing in label but not changing in form paint ..
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter = 1;
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
counter = 0;
timer1.Tick += new EventHandler(timer1_Tick);
counter = new int();
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson),200,200,counter ,300) ;
}
}
}
I intend to change my drawings gradient with time but variable is not changing when its
come to form paint... but it does change in lbl ...
help me if u can guys . dont know what to do.
Here, this one works. The answer is to call this.Invalidate() on form every timer tick.
public partial class Form1 : Form
{
int counter = 0;
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
counter = 1;
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson), 200, 200, counter*10, 300);
}
}
Also changed several things:
Event handler is set only once – to avoid multiple handlers if user clicks button several times.
Removed counter = new int() – no need, you have already set it to =1.
In Form1_Paint set x2 coordinate to counter*10 so it is easier to see the movement.
I will recommend following:
Use Panel control for Drawing and its paint event e.g. Panel_Paint
In Timer_Tick use Panel.Invalidate();
In the paint event Dispose for graphic object that is Pen.
Add a Panel control named panel1 in the form.
Keep every other control outside the panel.
Example of Panel Paint and Timer event:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Brushes.Crimson))
{
e.Graphics.DrawLine(pen, 200, 200, counter, 300);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
panel1.Invalidate();
}

Move images in C#

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form.
I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image.
How I can show image smoothly transition to the final location?
Here is my code so far:
public partial class Form1 : Form
{
private int counter = 0;
void timer_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pictureBox1.Show();
timer1.Stop();
counter = 0;
}
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
while(i<=100){
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
timer1.Start();
}
}
}
Does this work? Sorry, I can't test it where I am right now (on netbook without VS).
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}

C# - Make form semi-transparent while moving

Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move event with no luck.
I'm stuck, any help?
The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.
The ResizeEnd event fires after a form has finished moving, so something like this should work:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.5;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:
[STAThread]
static void Main()
{
using (Form form = new Form())
using (Timer tmr = new Timer())
{
tmr.Interval = 500;
bool first = true;
tmr.Tick += delegate
{
tmr.Stop();
form.Opacity = 1;
};
form.Move += delegate
{
if (first) { first = false; return; }
tmr.Stop();
tmr.Start();
form.Opacity = 0.3;
};
Application.Run(form);
}
}
Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.

Categories