C# 图像自动切换

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.Threading;
using System.IO;
using System.Runtime.InteropServices

namespace 自动切换图像
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private bool Stara_Stop = true;
string path1,path2;
int i = 1;
private void timer1_Tick(object sender, EventArgs e)//自动切换
{
try
{
if (!Stara_Stop && (i < (Convert.ToInt16(textBox1.Text) + 1)))
{

//Thread.Sleep(1000);
pictureBox1.Image = null;
path2 = path1 + i.ToString() + Image_format();
pictureBox1.Image = Image.FromFile(path2);
if (i == Convert.ToInt16(textBox1.Text))
{
i = 0;
}
i++;


}
}
catch (Exception ex)
{
timer1.Enabled = false;
MessageBox.Show(ex.Message,"error!",MessageBoxButtons.OK);
}
}

private void button1_Click(object sender, EventArgs e)//开始停止
{
if(m_Data_null())
{
if (Stara_Stop)
{
path1 = path();
button1.Text = "停止切换";
button1.BackColor = Color.Red;
Stara_Stop = false;
timer1.Enabled = true;
timer1.Interval = Convert.ToInt16(textBox3.Text);

textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
comboBox1.Enabled = false;
button2.Enabled = false;
}
else
{
button1.Text = "开始切换";
button1.BackColor = Color.White;
Stara_Stop = true;
timer1.Enabled = false;

textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
comboBox1.Enabled = true;
button2.Enabled = true;

}
}
}

 

private bool m_Data_null()
{
bool m_Data = false;
if (textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox3.Text != string.Empty
&& comboBox1.Text != string.Empty)
{
m_Data = true;
}
else
{
m_Data = false;
MessageBox.Show("请设置切换的图像及条件!", "提示!", MessageBoxButtons.OK);
}
return m_Data;
}


private string path()//图像路径
{
string m_path;
if (textBox1.Text == string.Empty)
{
m_path = @"D:\_VS2015_\图像自动切换\Image\robot\";

}
else
{
m_path = textBox2.Text+ @"\";
}
return m_path;
}

private string Image_format()//图像格式
{
string format;
format = comboBox1.Text;
return format;
}

private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = ".PNG";
}

private void button2_Click(object sender, EventArgs e)
{
i = 1;
pictureBox1.Image = null;
}

 

private void textBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
//选择打开文件
//OpenFileDialog of = new OpenFileDialog();
//if (of.ShowDialog() == DialogResult.OK)
//{
// textBox2.Text = of.FileName;
//}

//选择文件夹路径

FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = folderBrowserDialog.SelectedPath;
//获取文件数量
var flies = Directory.GetFiles(textBox2.Text);
textBox1.Text = (flies.Length).ToString();
}
}

private void button3_Click(object sender, EventArgs e)
{
this.Close();
}


}
}

 

 

 

//以下是UI界面

posted on 2018-01-05 11:04  Mr_Mo  阅读(1475)  评论(0编辑  收藏  举报

导航