Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / WPF

Another Screensaver with WPF

Rate me:
Please Sign up or sign in to vote.
4.87/5 (17 votes)
27 Jan 2012CDDL8 min read 71.9K   4.3K   63  
Lessons learnt from writing a screensaver with WPF
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using RZWScreenSaver.Properties;

namespace RZWScreenSaver.SlidePages{
    /// <summary>
    /// Interaction logic for SimpleSlide.xaml
    /// </summary>
    public partial class SimpleSlide{
        public SimpleSlide() {
            InitializeComponent();
            imagePathText.Visibility = Settings.Default.ShowTitle ? Visibility.Visible : Visibility.Collapsed;
        }
        public override bool ShowTitle{
            get { return imagePathText.Visibility == Visibility.Visible; }
            set{ imagePathText.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        protected override void OnPictureSetChanged(object sender, EventArgs arg){
            base.OnPictureSetChanged(sender, arg);
            slideShowImage.Source = null;
        }
        protected override void OnShowPicture(object sender, PictureChangedEventArgs arg){
            base.OnShowPicture(sender, arg);
            var image = arg.Picture;
            var orientation = GetImageOrientation(image);

            imagePathText.Content = FormatImageDescription(image, arg.Path, arg.FileDate, orientation);

            switch (DisplayMode){
            case DisplayMode.OriginalOrFit:
                slideShowImage.Stretch = canImageFitScreen(image, orientation)? Stretch.None : Stretch.Uniform;
                break;
            case DisplayMode.OriginalOrFillCrop:
                slideShowImage.Stretch = canImageFitScreen(image, orientation) ? Stretch.None : Stretch.UniformToFill;
                break;
                // default just ignore.
            }
            slideShowImage.Source = image;
            imageOrientation.Angle = -(orientation ?? 0);
            if (!IsLandscape(orientation)){
                var parentWindow = (FrameworkElement) Parent;
                slideShowImage.Width = parentWindow.Height;
                slideShowImage.Height = parentWindow.Width;
            } else{
                slideShowImage.Width = slideShowImage.Height = double.NaN;
            }
        }
        protected override void OnDisplayModeChanged() {
            base.OnDisplayModeChanged();
            switch (DisplayMode){
            case DisplayMode.Original:
                slideShowImage.Stretch = Stretch.None;
                break;
            case DisplayMode.Stretch:   
                slideShowImage.Stretch = Stretch.Fill;
                break;
            case DisplayMode.Fit:
                slideShowImage.Stretch = Stretch.Uniform;
                break;
            case DisplayMode.FillCrop:
                slideShowImage.Stretch = Stretch.UniformToFill;
                break;
            case DisplayMode.OriginalOrFit:
            case DisplayMode.OriginalOrFillCrop:
                // effect takes on changing image.
                break;
            default:
                Trace.WriteLine("Unhandled mode: " + DisplayMode);
                break;
            }
        }
        bool canImageFitScreen(ImageSource image, int? orientation){
            Size imageSize;
            GetImageSizeByOrientation(image, orientation, out imageSize);

            var parentWindow = Parent as FrameworkElement;
            return parentWindow != null && imageSize.Width <= parentWindow.Width && imageSize.Height <= parentWindow.Height;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Architect
Thailand Thailand
C/C++ and C# programmer.

Comments and Discussions