Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Another Screensaver with WPF

, 27 Jan 2012
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)

Share

About the Author

Ruxo Zheng
Technical Lead
Thailand Thailand
C/C++ and C# programmer.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid