GIF Animation not working in Windows Form - c#

I have 2 WinForms
Form2 uses Infragistics ultraTabControl.
On Tab Changing im Showing Form1.
In Form1
I have a PictureBox assigned with animated GIF
In Form2
I am displaying Form1 like this.
Form1 frmOne=new Form1();
frmOne.Show();
Problem
GIF is not playing animation.

Make sure you have your image in the image property NOT THE BACKGROUND IMAGE PROPERTY. That's the mistake I made

It is working fine for me. I have just dragged a new pictureBox on form and setting image property at Form_Load() event and showing GIF animated.
I did same as you on button click:
TestForms.NavBarTest newForm = new TestForms.NavBarTest();
newForm.Show();
At my Test Form:
private void NavBarTest_Load(object sender, EventArgs e)
{
pictureBox1.Image = NavronChartControl.Properties.Resources.url;
}
Note: If your picture box is disabled then it will not animate the Gif
Reference:
How do you show animated GIFs on a Windows Form (c#)
Animated Progress Indicator in C# (Windows Forms)
Try to implement using this link:Animated GIF in picturebox won't animate apprach.

The best way to achieve it is to run the animation in an async task, but accordingly, some limitations are possible to do that on windows form using:
System.Threading.Thread.Sleep(int milliseconds).
My splash view is displayed with a gif (loading)
e.g.: In your constructor,
public partial class MainMenu : Form
{
private SplashScreen splash = new SplashScreen();
public MainMenu ()
{
InitializeComponent();
Task.Factory.StartNew(() => {
splash.ShowDialog();
});
Thread.Sleep(2000);
}
It is imperative to put the Thread.Sleep(int) after starting a new one, don't forget that every action you did on this thread needs to be invoked, for example:
void CloseSplash()
{
Invoke(new MethodInvoker(() =>
{
splash.Close();
}));
}
Now your gif should work!

Solved
My current Thread is busy to Play GIF Animation.
I tried So many ways like Application.DoEvents(); etc.
But every thing can't helped me.
The answer in the following Question, which uses Threading is a very great working idea.
StackOverFlow: Show/Hide Splash Screen
Thanks for every one.

This question has already raised before.
Though the approach is different but the questions concept is the same
StackOverFlow: How do you show animated GIFs on a Windows Form (c#)
Source project where you can use Code Project
I guess that will help you out

I had the same issue and came across different solutions by implementing which I used to face several different issues. Finally, below is what I put some pieces from different posts together which worked for me as expected.
private void btnCompare_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Execution);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
Here is the Execution method that also carries invoking the PictureBox control:
private void Execution()
{
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
Application.DoEvents();
// Your main code comes here . . .
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
}
Keep in mind, the PictureBox is invisible from Properties Window or do below:
private void ComparerForm_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

If none of the previous answers work for you, another idea is to refresh the image tag or write it on the fly with jQuery.
In my case I needed to show an animated GIF when a button on the form was clicked. Previously it would show up but not animate. I just added a jQuery event when the button was clicked that added the image tag on-the-fly to a div through the parent tag's html() function.
$('.showhidegif').html("<img src='/Content/img/angled-spinner.gif' />");
<div class="col-sm-12 showhidegif"></div>
Worth a shot especially if you're already using jquery on your page.

Related

How to change a video once the current has stopped?

I am currently working with .NET 4.5. Programming a photobooth. The photobooth should start wit fullscreen:
Show an Intro screen with an intro video.
When the user clicks on the screen, it has to show a get ready! video
When the get ready video finishes playing, it has to show a countdown video
When the countdown video finishes, it has to take a picture.
Right now I can do #1 and #2. I can also take pictures. My problem is with the sequence. Once a video finishes, I need to change the video, but since everything runs in the event handler, it only plays the video WHILE the program is running the event handler.
Is there any better and smarter way to do this?
Here is my code:
What I have and works right now:
private void Form1_Load(object sender, EventArgs e)
{
// maximize the screen
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
// play an intro video without the UI controls
this.videoPath = this.basePath + "intro.mp4";
this.wmpVideo.URL = this.videoPath;
this.wmpVideo.settings.setMode("Loop", true);
this.wmpVideo.Ctlcontrols.play();
this.wmpVideo.uiMode = "none";
}
Then, if the user clicks on the video, it will change the video to a new file:
private void wmpVideo_MouseDownEvent(object sender, AxWMPLib._WMPOCXEvents_MouseDownEvent e)
{
if (this.current == 0) //this means that currently we are in the intro
{
// Then I change the video to "get ready!"
this.changeVideo("getready.mp4",1);
}
changeVideo basically updates the video to getready.mp4. Then, I need it to change it to countdown.mp4.
In order to move from one video to another, I need to check when the video finishes. I do this right now with:
wmpVideo.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(checkGetReadyFinished);
And the event handler looks like this:
private void checkGetReadyFinished(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8) {
// If we are currently in the "getready state"
if (this.current == 1)
{
// then I change the video to the countdown.mp4
this.changeVideo("countdown.mp4", 2);
}
}
}
PROBLEM
I noticed the video ONLY plays when I am on the event handler method. I realized this because I saw that no video was played, so I added a MessageBox in the changeVideo method. I saw that as long as the MessageBox was showing, the video was playing. When I clicked "OK", the video disappeared
I.E., I need to stay in the event handler method.
QUESTION
Is there any other better way to show a video1, then show video2 when video1 finishes. As I mentioned, I need to do a photobooth program.
I also tried calling another form, but as soon as I set it to fullscreen, the screen sort of flashes.
Is there any optimal way of doing this?

Clean and reset a form without leaving it with C#

Plop,
I'm currently working on a Hangman game with C#. I use a main form to the menu, then I start another form within the first form in that way :
Form2 game = new Form2();
game.ShowDialog();
So when I finish a game, I don't want to close it form, but I want to reload the whole form. Here is the way I do :
private void Replay(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
Form2_Load(this, null);
}
This code removes all Controls then recreate them with InitializeComponent. I thought removing controls would clean the screen but it doesn't. Some labels and PictureBox that are removed still stay displayed.
Where am I doing this wrong ?

C# Form Background Parameter

I have created a simple C# application to automatically login into a hotspot. I have a notify icon with a contextmenustrip with some functions like Connect, Disconnect, etc. I want to be able to run the form in the background showing only the notify icon to automatically login into the hotspot if the form is hidden.
I followed the instructions from VBNight in this post:
Hide form at launch
The application is running in the background, the notify icon showing but the Form_Load function is not working until I press on the notify icon.
I guess that's because Form_Load Occurs before a form is DISPLAYED for the first time.
Try moving your code from Form_Load to the constructor after InitializeComponent(); or so.
EDIT:
To answer your question, I suggest you extract code #1 from...
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// move this code #1 to...
}
and move the code to a brandnew method.
private void NewButtonClicked() {
// move code #1 here (in case)
}
Then, go back and call the method you just created.
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// You can leave code #1 but to remove duplicate,
NewButtonClicked();
}
Finally, replace YOUR_BUTTON.PerformClick(); with NewButtonClicked(); wherever you need. I assume you don't need any interaction with form Controls since the form is hidden.
I fixed it changing the form opacity to 0 and the ShowInTaskbar property to false. The form is hidden and the code is working.

Creating a Pop-Up form in C#

I am trying to create a windows form that gets displayed for 2 seconds when triggerd by an event, and then closes automatically.
I have tried several options. This is my current code:
this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.Show();
Thread.Sleep(2000);
this.aletPopup.Close();
This preforms the actions that I desire, however, when the form loads it does not load the label or image which is on the form. Instead, the area where the image and label are become transparent. My desired output is this:
I have also tried using this.aletPopup.ShowDialog();, which does display the graphics. However, the form will not close automatically when using this method.
EDIT: I am attempting to use
Michael Perrenoud's solution. However, I cannot get the form to close. I have a timer set at a 2000ms interval which is initally disabled. Am I overriding the OnShown correctly?
public AlertPopForm()
{
InitializeComponent();
}
private void closingTimer_Tick(object sender, EventArgs e)
{
closingTimer.Enabled = false;
this.Close();
}
private void AlertPopForm_OnShown(object sender, System.EventArgs e)
{
closingTimer.Enabled = true;
closingTimer.Start();
}
Instead, how about leveraging ShowDialog, and then using a Timer on the dialog form. In the Tick event of the Timer, close the form.
this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.ShowDialog();
You could even pass the interval into the .ctor of the dialog form if you wanted some flexibility.
It's very important to note here that you'd need to leverage the OnShown override to actually Start the Timer so the form is in fact shown to the user.
The reason can be in Message Loop. When you block your thread by Thread.Sleep, it also blocks Message loop.
You can make like this:
this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.Show();
for(var i = 0; i<= 200; i++)
{
Thread.Sleep(10);
Application.DoEvents();
}
this.aletPopup.Close();
DoEvents will process messages from message queue during that time.
When calling Thread.Sleep you're blocking the UI thread, thus preventing it from processing UI events.
You need to ensure that Close is called after 2 seconds without actually blocking the main thread. There are a number of ways of doing this, such as using a Timer, or something like Task.Delay:
aletPopup.StartPosition = FormStartPosition.CenterScreen;
aletPopup.Show();
Task.Delay(TimeSpan.FromSeconds(2))
.ContinueWith(t => aletPopup.Close(),
TaskScheduler.FromCurrentSynchronizationContext());
The reason this is happening, is that you are halting the thread that draws the form. So the form has time to display, but as it's being drawn, the thread is being stopped.
Easy enough to fix....
Add an event handler to the popup for the Load event with the following handler:
private async void handleLoad(Object sender, EventArgs args)
{
await Task.Delay(2000);
Close();
}
Remark
Because you used Show(), the user could always click around this popup. If this is undesirable, then use ShowDialog() instead.
Did you try a refresh to redraw the form?
this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.Show();
this.alertPopup.Refresh();
Thread.Sleep(2000);
this.aletPopup.Close();

Change duration of splash screen?

I recently added a splash screen to my WPF app.My app loads fast so the screen is only on for milliseconds.How would i lengthen the time my splash screen stays up.I would like it to be two seconds.
If you trigger the splash screen to show in the Application.Startup Event you will have full control over it. (be sure to call .Show() with false)
private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreen screen = new SplashScreen("splashScreen.png");
screen.Show(false);
}
Then you can call screen.Close() when you would like the splash screen to close.
You can also call
System.Threading.Thread.Sleep() before the InitializeComponent in the main window. this works.
something like that:
public MainWindow()
{
System.Threading.Thread.Sleep(2000);
InitializeComponent();}
The best way and using the API is
SplashScreen splash = new SplashScreen("splashscreen.jpg");
splash.Show(false);
splash.Close(TimeSpan.FromMilliseconds(2));
InitializeComponent();

Categories

Resources