我有一个包含 1000 张图像的文件夹,图像名称的顺序为“ICON000、ICON001 直到 ICON 999”。我需要它们以 5 秒的时间延迟按顺序显示在我的 WPF 图像控件中。我使用文件对话框来获取特定文件夹的路径和图像的相应前缀(图标)。我使用了下面的代码
string path = null;
string selected_file;
string URI;`enter code here`
openfile.AddExtension = true;
openfile.Filter = "GIF(*.gif)|*.gif|PNG(*.png)|*.png|JPG(*.jpg)|*.jpg|JPEG(*.jpeg)|*.jpeg";
DialogResult result = openfile.ShowDialog();
if (result.ToString() == "OK")
{
selected_file = openfile.FileName;
Uri uri_temp = new Uri(selected_file);
URI = uri_temp.AbsoluteUri;
string[] ext = URI.Split('.');
//textBox1.Text = ext[0];
string[] ss = ext[0].Split('/');
int a = ss.Length;
string a1 = ss[a - 1];
string image_prefix = a1.Substring(0, 4);
string image_no = a1.Substring(4, 3);
for (int i = 0; i < a-1; i++)
{
path = path + ss[i] + "/";
}
string path1 = path;
path = path1 + image_prefix + image_no + "." + ext[1];
for (int i = 1; i < 999; i++)
{
if (i < 10)
{
image_no = "00" + i;
}
else if (i < 100)
{
image_no = "0" + i;
}
else
{
image_no = i.ToString();
}
path = path1 + image_prefix + image_no + "." + ext[1];
string dasdasd = path;
string loc = new Uri(path).LocalPath;
bool asasa = File.Exists(loc);
if (asasa == true)
{ System.Threading.Thread.Sleep(5000);
image1.Source = new BitmapImage(new Uri(dasdasd));
}
else
{
System.Windows.Forms.MessageBox.Show("File not found");
}
但图像没有显示。做需要的事情....!!
Use a 调度定时器 http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx更新当前显示的图像。
private DispatcherTimer timer = new DispatcherTimer();
private int imageIndex;
private int maxImageIndex;
public MainWindow()
{
InitializeComponent();
timer.Tick += TimerTick;
}
private void StartSlideShow(TimeSpan interval, int maxIndex)
{
imageIndex = 0;
maxImageIndex = maxIndex;
timer.Interval = interval;
timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
image.Source = new BitmapImage(new Uri(CreatePath(imageIndex)));
if (++imageIndex >= maxImageIndex)
{
((DispatcherTimer)sender).Stop();
}
}
private string CreatePath(int index)
{
// create image file path from index
// ...
}
例如通过调用开始显示图像
StartSlideShow(TimeSpan.FromSeconds(5), 1000);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)