C# Timer not working when using animated gif - c#

I am busy with creating an GUI which should have some animations in it. My idea was to use an animated gif as background with controls on top. This all is working fine until I wanted to add an timer to update some values each second, then the timer does not work. If I set enabled setting of the picture box to false the timer is running. For me it sounds like having some performance issues, although 140mb of ram and only a few percent of CPU is used.
Both the picture box and timer are the standard from Visual Studio 2019, where I program in C#.
The animated gif is 50mb and is in the Systems.Windows.Forms.PictureBox.
The timer I have used is the standard timer: System.Windows.Forms.Timer
Is there an timer that does work in combination with the animated gif? Or is there a picture box which can process animated gifs with better performance? Or should I step out from an animated gif to something else?
Currently the code is nothing more then changing the text of a button:
Currently it is nothing more then changing the text of an button:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "1234";
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Text = "test";
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Start();
}
Settings of the timer and animated gifs etc are in the proporties of that container.
Thanks in advance.
Edit: allready tried compressing the gif, with this the image is still around 40m and has the same problem. Smaller will result in too much loss of quality. An real small animated gif does not give the issue. So is there a workaround to use the big animated gif? :)
To give an idea what I would like to achieve:
https://motionarray.com/stock-motion-graphics/hud-video-frame-345823
This I would like to use with buttons and sliders on top.

Firstly, I'd suggest trying to compress your animated gif down. 50MB is large, and a quick Google search will show you many different sites that will compress it down to something smaller. Having a gif that size in your program is giving you performance issues.
I quickly recreated a small example with an animated gif with two buttons and a timer control, and the timer was ticking fine without the background messing up its animation. Make sure you have the timer set to disabled in the Properties tab, and then simply set the timer to enabled when you want it to start ticking.
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "1234";
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Text = "test";
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

Related

C# Winforms Usercontrol drag and snap to panel (runtime)

I am trying to write a program to manage tasks from my team. For this I use drag&drop capable custom UserControls. I try the following: When the UserControl is dragged over a slot, the control should then snapped into the slot. The problem seems to be that the slot does not get the events like "MouseEnter" or "DragEnter". Does anyone know a workaround, or is it possible to pass events down?
I think I just need a little push in the right direction.
Edit:
Main functionality:
The user control, let's call it task, should be docked into a slot when released over it.
A slot consists of properties and a panel. The task should appear in the panel.
Bonus:
A task, should dock into the different slots while dragging. So that it is always visible where the 'task' is placed when it is released.
I hope this shows what I am trying to do.
Edit 2
To clarification: I use the "MouseDown", "MouseUp" and "MouseMove" for the drag an drop.
Here you can see my Example the blueish panel is my task. The white the slot it should snap to.
On the second picture I drag the Task over the Slot panel. The mouse is not visable here but it's right beside the "K" of task.
i have added for testing the following code. Which should color the panel inside the slot in a color if an event is raised. But the color stays white.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
this.panel1.BackColor = Color.Black;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
this.panel1.BackColor = Color.Green;
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
this.panel1.BackColor = Color.Yellow;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
this.panel1.BackColor = Color.Violet;
}
Why I don't get events on the Slotpanel?
I hope that this helps to understand my problem.

Label.Image gradually increase with for loop

Can't figure out why image in label1 connected with imageList1 doesn't want to change more than once after pressing of a mouse button. Imagelist consists of 7 images which I want to have gradually appear in label element...that was the whole idea.
private void button1_Click(object sender, EventArgs e)
{
int number = 0;
for (int i = 0; i < imageList1.Images.Count; i++)
{
label1.Image = imageList1.Images[number++];
}
}
The default ImageIndex in label1 properties is set to 0 (first image) and after the for loop it gets to index1.
I am guessing that the last image stays? If you want the images to appear one by one with a timeout you should do something like
private async void button1_Click(object sender, EventArgs e)
{
foreach (Image image in imageList1)
{
await Task.Delay(1000); //wait for one second before changing
label1.Image = image;
}
}
Of course depending on your requirements you may want to disable the button and as pointed out by #nvoigt you may want to use some animation capabilities of the UI framework.
Your form will only repaint once the whole button event ran. That means you will only ever see the last image. Look into background workers or maybe timers to have animation. Maybe WPF is the way to go if animation is the main purpose of your program.
Your loop does assign the images OK but there is no time to show them because updating the UI is not happening before the loop is through.
You could force the UI update by inserting an Application.DoEvents()
label1.Image = imageList1.Images[number++];
Application.DoEvents();
You can try it but you should not actually use this as your solution! It has two serious issues, none of which you want:
It gives you no control over the animation speed.
Application.DoEvents can introduce serious problems in your code and you should not get into the habit of using it at all. Look it up or just believe it!
The best way to do any animation in Winforms is to use a Timer. In the Button click you set it up and start it. In its Tick you do the animation..
Have alook at this post for a button animation example! Instead of Mouse_Enter use your button click. Stop the Timer when the images have all been shown!
If all you want to do is playing around a little getting used to Timers is highly recommended and there is no need at all for WPF. If you will need a lot of high class animation WPF is indeed the way to go.
Here is the code to a minimal solution:
Timer timer1 = new Timer();
int imageIndex = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (imageIndex >= imageList1.Images.Count ) timer1.Stop();
label1.Image = imageList1.Images[imageIndex++];
}
private void button1_Click(object sender, EventArgs e)
{
imageIndex = 0;
timer1.Interval = 100; // change ms to suit your needs!
timer1.Start();
}
There are various issues with your code.
By incrementing number inside the loop, you force it to have the same value as i. If that's what you want to do, why not simply use i?
You never let the UI thread update the display. The UI thread will update the display only after the event handler finishes.
The result is that only the last image will be displayed.
To allow the UI thread to update the display, you need to use Application.DoEvents, eg:
foreach(Image image in imageList1.Images)
{
label1.Image = image;
Application.DoEvents();
}
Of course this will just go through all the images at once, so you'll just see a blur.
If you want a simple, smooth animation, use an [animated GIF2 or use WPF. Trying to do this in Windows Forms with individual images is not straightforward.
If you want to show an animation, you can put a delay as #Stilgar suggests, although this won't guarantee a smooth animation. Thread switching or high CPU load means that the delay between images will always be greater than the delay amount. The result will be a jerky animation.
You can use a timer event to update the image. This is better, but high CPU load can still delay processing of the event. Only WPF can guarantee the animation will be smooth without complex coding.

How to make picturebox change image smooth and fast

I got a picture box that got an original image in my program. When i hover with my mouse over it it changes picture to another picture and when i leave it changes back.
But..
There is such a delay to change to the right picture if i hover over it or not. It takes like 1 second before it changes, What should i change to improve the speed of the change? This is the code i am using at the moment:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörn_1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörnet;
}
This is the third time today that I've seen this issue. MouseHover is raised when the mouse pointer STOPS OVER a control. If you want something to happen as soon as the mouse pointer goes over the control then you want MouseEnter, just as you're using MouseLeave for the change back again.

Form gets bigger after saving size and location properties

Something is wrong with how settings are being saved/read from in Windows Forms apps.
It's not a problem with my code. But I don't know what's wrong.
Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(Properties.Settings.Default.Size.Width, Properties.Settings.Default.Size.Height);
this.Location = new Point(Properties.Settings.Default.Location.X, Properties.Settings.Default.Location.Y);
}
Form1_Closing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Location = this.Location;
Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.Save();
}
Now. You can see that the code is right.
Why then, does the Form get bigger and Bigger and BIgger and BIGger and BIGGer and BIGGEr and BIGGER every single time I reopen my app?
It's supposed to stay the same size! You know, since I saved it and everything... Why?
The Size property includes the non-client area of the form, which can fluctuate wildly depending on whether you have enabled themes, Aero, etc.
Try using the ClientSize property instead.

C# youtube video not playing anymore

Hello I have an application that used to allow me to watch videos from youtube, but now the screen is completely black, I cannot view videos. I am using axmediaplayer and here is my code.
private void Form1_Load(object sender, EventArgs e)
{
// play video in embedded media player
axWindowsMediaPlayer1.URL = "http://www.youtube.com/v/Of9JCZ6zraQ";
}
Is it a change made on youtube or does any one know alternatives? it used to work before but not it is not working any more. does any one know what the problem is please?

Categories

Resources