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 CDDL
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() {
            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;
            case DisplayMode.OriginalOrFillCrop:
                slideShowImage.Stretch = canImageFitScreen(image, orientation) ? Stretch.None : Stretch.UniformToFill;
                // 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() {
            switch (DisplayMode){
            case DisplayMode.Original:
                slideShowImage.Stretch = Stretch.None;
            case DisplayMode.Stretch:   
                slideShowImage.Stretch = Stretch.Fill;
            case DisplayMode.Fit:
                slideShowImage.Stretch = Stretch.Uniform;
            case DisplayMode.FillCrop:
                slideShowImage.Stretch = Stretch.UniformToFill;
            case DisplayMode.OriginalOrFit:
            case DisplayMode.OriginalOrFillCrop:
                // effect takes on changing image.
                Trace.WriteLine("Unhandled mode: " + DisplayMode);
        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.


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


About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid