Hey there,
Over the past few weeks as an excerise I've been trying to create a C# image viewer. At the moment I have one window, with a list of images displayed in a ListBox.
When I the F5 button, a second window pops up at fullscreen, and I want it to cycle through all the images shown in the previous screen. At the moment, the picture folder is simply My Pictures.
In order to simulate a cross-fade effect, I have two image objects on my canvas, PreviewImages and Background. The idea is that Image 1 = Background.Source
whilst PreviewImages.Source = Image 2
. Because I have a DoubleAnimation fade in effect, it will appear like image 1 and 2 are crossfading. The script then leads onto the next image in the series after a Task.Delay.
HOWEVER, my issue is that for some reason I cannot explain, after a while operating the code (say 10 minutes) instead of showing a ncie cross fade, the code plays up and starts skipping images and the cross fade completely breaks, instead it appears like Background.Source becomes some random image, so the result is an instant snap to an unrelated image, followed by an instantaneous the fade in of the "correct" image.
The code I am using for this is:
public async void transition()
{
while (App.timer = true)
{
for (App.index = 0; App.index < AllImages.Count; App.index++)
{
DoubleAnimation fadeoutAnimation = new DoubleAnimation();
fadeoutAnimation.Duration = TimeSpan.FromSeconds(1.0);
fadeoutAnimation.From = 0;
fadeoutAnimation.To = 1;
PreviewImages.BeginAnimation(Image.OpacityProperty, fadeoutAnimation);
await Task.Delay(5000);
PreviewImages.Source = new BitmapImage(new Uri(Convert.ToString(AllImages[App.index]._image)));
if (App.index < (AllImages.Count - 1) && App.index > 0)
{
App.index2 = App.index - 1;
}
else if (App.index < 1)
{
App.index2 = (AllImages.Count - 1);
}
Background.Source = new BitmapImage(new Uri(Convert.ToString(AllImages[App.index2]._image)));
}
}
}
The list of images is populated by:
public List<MyImage> AllImages
{
get
{
List<MyImage> result = new List<MyImage>();
foreach (string filename in
System.IO.Directory.GetFiles(
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))
{
try
{
result.Add(
new MyImage(
new BitmapImage(
new Uri(filename))));
}
catch { }
}
return result;
}
}
Is someone able to tell me if there are any mistakes here? I find it incredibly odd that it starts by working fine, but only after a while starts going wrong. That makes me think its an issue with the App.index++