How to stop GIF at specific Frames in winfoms - c#

basically my question is how to stop GIF at specific frame.
i have a picture box that is already set its image with GIF and i created a 2 events Enter and Leave
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODEFAULT1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}
what i want is to stop the GIF at last frame(all gif's consist of 7 frames)
i tried the ImageAnimator and ImageStop
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
ImageAnimator.Animate(pictureBox1.Image, OnFrameChanged1);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(pictureBox1.Image, OnFrameChanged2);
}
private void OnFrameChanged1(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODENTER;
}
private void OnFrameChanged2(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}

I think you can use the Image.SelectActiveFrame Method.

Related

Upload Code to Arduino UNO with a C# Form

I build a project in C# with which one can control a RGB led. My goal is to build a function in which one can upload code that controls the LED. Therefore I need help with the following proble :
There is a button and a rich text box on the form. The text of the rich text box contains source code, which should get uploaded to the arduino if you press the button. I know how to open the port but i dont know how to upload the source code.
My code :
namespace RGBLedColorPicker
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort("COM3", 9600);
private Color defaultColor = Color.FromArgb(0, 0, 0);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
port.Open();
this.SetColor(defaultColor);
}
catch
{
MessageBox.Show("Cant find Arduino.Closing programm");
Application.Exit();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.SetColor(defaultColor);
port.Close();
}
private void panel1_Click(object sender, EventArgs e)
{
}
private void SetColor(Color color)
{
port.Write(new[] { color.R, color.G, color.B }, 0, 3);
}
private void button1_Click(object sender, EventArgs e)
{
this.SetColor(Color.Red);
}
private void button2_Click(object sender, EventArgs e)
{
this.SetColor(Color.Green);
}
private void button3_Click(object sender, EventArgs e)
{
this.SetColor(Color.Blue);
}
private void button4_Click(object sender, EventArgs e)
{
this.SetColor(Color.Red);
Thread.Sleep(2000);
this.SetColor(Color.Green);
Thread.Sleep(2000);
this.SetColor(Color.Blue);
Thread.Sleep(2000);
MessageBox.Show("Show beendet","beendet",MessageBoxButtons.OK);
this.SetColor(defaultColor);
}
private void button5_Click(object sender, EventArgs e)
{
using (Create f2 = new Create())
{
f2.ShowDialog(this);
}
}
private void button6_Click(object sender, EventArgs e)
{
this.SetColor(defaultColor);
}
private void button7_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
SetColor(colorDialog1.Color);
}
}
}
}

Get an image from resources of project

I have RadioButton which has a background. Now I need to change background on MouseEnter event. I can do that with
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = Image.FromFile("D:/img/sample.png");
}
But I already have that image as resource in project and I don't know how to get to it.
try as follow:-
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = <YourNameSpace>.Properties.Resources.<ResourceName>;
}
In case you use winforms, include the images to your resource:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}

Calling a text box value when the progress bar finishes

I am new to C#.
Just doing my first project.
This is my code.
I just want to fill the textbox once the progress bar finish loading.
But its not working now.
Can anyone tell me what I am doing worong.
My code is as below.
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
int m = progressBar1.Value;
if(m==0)
{
textBox2.Text = "test";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
Try This:
int progressVal=0;
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
progressVal= progressBar1.Value;
if(progressVal==progressBar1.Maximum)
{
timer1.Stop();
textBox2.Text = "Loading done!";
}
}

DragMove prevent preview mouse up

I have an application that I want to move the window the problem is that all images that had a preview mouse up now are not working.
This is the windo event:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
and this is the image event:
private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("WTF IS WPF?");
}
If I remove the DragMove function the image event works.
why execute DragMove() all the time?
MouseButtonState _mouseButtonState;
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
_mouseButtonState = e.ButtonState;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if(_mouseButtonState == MouseButtonState.Pressed)
DragMove();
}
I would also put a check in image1_PreviewMouseUp
private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if(_dragging) return;
//else do your preview
}

Calling code of Button from another one in C#

I need to know if it's possible to call the Click of a button from another one.
private void myAction_Click(object sender, EventArgs e)
{
// int x;
// ...
}
private void Go_Click(object sender, EventArgs e)
{
// call the myAction_Click button
}
Thanks.
You want:
private void Go_Click(object sender, EventArgs e)
{
myAction_Click(sender, e);
}
But a better design is to pull the code out:
private void myAction_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Go_Click(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// Your code here
}
The button has a PerformClick method.
http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx

Categories