Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pause and Resume WPF Slideshow

Posted on 2009-05-12
3
Medium Priority
?
2,397 Views
Last Modified: 2013-12-17
hi , I am attaching a code , to play a slideshow of pictures from your "MyPictures" folder of your PC

Create a new project of the type "WPF Application" and replace window1.xaml and window1.xaml.cs code with my code below

I would like to implement a funtionality to Pause and Resume WPF Slideshow
When the slide show is paused, a text on the top right corner should be displayed as "Paused"

Make Sure you have pictures in "MyPictures" folder of your PC
Please help me implement these events...
//In window.xaml
 
<Window x:Class="MySlideShow.Window1"
        x:Name="Window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="My SlideShow" Height="480" Width="640" WindowStartupLocation="CenterScreen" WindowState="Maximized" Background="#FF000000">
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Rectangle x:Name="rec1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        <Rectangle x:Name="rec2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Window>
 
 
// window1.xaml.cs code
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
 
namespace MySlideShow
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
 
        string path;
        string[] files;
        List<string> showPictures = new List<string>();
        int count = 0;
        DispatcherTimer dt = new DispatcherTimer();
        int currentRect = 1;
        double fade = 0.5;
 
        public Window1()
        {
            InitializeComponent();
            this.Show();
 
            //Get The Picture files from the My Pictures Directory
            files = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
 
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].ToUpper().EndsWith(".JPG") ||
                    files[i].ToUpper().EndsWith(".PNG") ||
                    files[i].ToUpper().EndsWith(".GIF") ||
                    files[i].ToUpper().EndsWith(".TIF"))
                {
                    showPictures.Add(files[i]);
                }
            }
 
            dt_Tick(this, new EventArgs());
            dt.Interval = TimeSpan.FromSeconds(2);
            dt.Tick += new EventHandler(dt_Tick);
 
        }
 
        void dt_Tick(object sender, EventArgs e)
        {
            if (showPictures.Count < 1)
            {
                MessageBox.Show("No Images Available");
                this.Close();
                dt.Stop();
            }
            else
            {
                Image img = new Image();
                img.Source = new BitmapImage(new Uri(showPictures[count]));
 
                if (currentRect == 1)
                {
                    rec1.Opacity = 0;
                    VisualBrush vb = new VisualBrush(img);
                    vb.Stretch = Stretch.UniformToFill;
                    rec1.Fill = vb;
                    rec1.BeginAnimation(FrameworkElement.OpacityProperty, new DoubleAnimation(1, TimeSpan.FromSeconds(fade)));
                    rec2.BeginAnimation(FrameworkElement.OpacityProperty, new DoubleAnimation(0, TimeSpan.FromSeconds(fade)));
                   
                }
                else
                {
                    rec2.Opacity = 0;
                    VisualBrush vb = new VisualBrush(img);
                    vb.Stretch = Stretch.UniformToFill;
                    rec2.Fill = vb;
                    rec2.BeginAnimation(FrameworkElement.OpacityProperty, new DoubleAnimation(1, TimeSpan.FromSeconds(fade)));
                    rec1.BeginAnimation(FrameworkElement.OpacityProperty, new DoubleAnimation(0, TimeSpan.FromSeconds(fade)));
                }
                dt.Start();
                if (currentRect == 1)
                {
                    currentRect = 2;
                }
                else { currentRect = 1; }
            }
            if (count == showPictures.Count - 1)
            {
                count = 0;
            }
            else
            {
                count++;
            }
        }
    }
}

Open in new window

0
Comment
Question by:ctrlctrl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
rendaduiyan earned 2000 total points
ID: 24372175
your implementation is based on a  timer. if you want to pause, you have to stop the timer.
So you have to write a event handler for KeyPressed or mouse button, in which you can stop the timer.
something like this.

public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_KEYDOWN)
            {
                return HandleKeys((Keys)((int)m.WParam) & Keys.KeyCode);
            }
            return false;
        }
public bool HandleKeys(Keys keycode)
        {
            bool ret = true;
            switch (keycode)
            {
                case Keys.P:dt.stop; break;
                case Keys.S:dt.start;break;
            }
            return ret;
        }

Open in new window

0
 

Author Comment

by:ctrlctrl
ID: 24374397
Hi rendaduiyan,


Thank You for the solution,

But will the slideshow resume at the same picture where it paused? with this implementation
0
 

Author Comment

by:ctrlctrl
ID: 24377158
excellent .. It worked
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question