첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    FontFamily="맑은 고딕"
    FontSize="12pt"
    Title="이미지 슬라이드 쇼 보여주기">
    <Window.Resources>
        <Storyboard x:Key="CurrentImageStoryboardKey"
            Storyboard.TargetProperty="(Image.Opacity)">
            <DoubleAnimation
                Storyboard.TargetName="currentImage"
                Duration="0:0:0.5"
                To="1" />
            <DoubleAnimation
                Storyboard.TargetName="nextImage"
                Duration="0:0:1"
                To="0" />
        </Storyboard>
        <Storyboard x:Key="NextImageStoryboardKey"
            Storyboard.TargetProperty="(Image.Opacity)">
            <DoubleAnimation
                Storyboard.TargetName="nextImage"
                Duration="0:0:0.5"
                To="1" />
            <DoubleAnimation
                Storyboard.TargetName="currentImage"
                Duration="0:0:1"
                To="0" />
        </Storyboard>
    </Window.Resources>
    <Grid>
        <Image x:Name="currentImage"
            Stretch="Fill"
            Opacity="1" />
        <Image x:Name="nextImage"
            Stretch="Fill"
            Opacity="0" />
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 비트맵 이미지 리스트
        /// </summary>
        private List<BitmapImage> bitmapImageList = new List<BitmapImage>();

        /// <summary>
        /// 디스패처 타이머
        /// </summary>
        private DispatcherTimer dispatcherTimer = new DispatcherTimer();

        /// <summary>
        /// 현재 이미지 스토리보드 시작 여부
        /// </summary>
        private bool startCurrentImageStoryboard = false;

        /// <summary>
        /// 현재 인덱스
        /// </summary>
        private int currentIndex = 0;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dispatcherTimer.Interval = TimeSpan.FromSeconds(2);

            this.dispatcherTimer.Tick += dispatcherTimer_Tick;

            this.dispatcherTimer.Start();

            SetBitmapImageList();
        }

        #endregion
        #region 디스패처 타이머 틱 발생시 처리하기 - dispatcherTimer_Tick(sender, e)

        /// <summary>
        /// 디스패처 타이머 틱 발생시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            this.currentIndex++;

            if(this.currentIndex > this.bitmapImageList.Count - 1)
            {
                this.currentIndex = 0;
            }

            if(this.startCurrentImageStoryboard)
            {
                this.currentImage.Source = this.bitmapImageList[this.currentIndex];

                (Resources["CurrentImageStoryboardKey"] as Storyboard).Begin(this);
            }
            else
            {
                this.nextImage.Source = this.bitmapImageList[this.currentIndex];

                (Resources["NextImageStoryboardKey"] as Storyboard).Begin(this);
            }

            this.startCurrentImageStoryboard = !this.startCurrentImageStoryboard;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 리소스 URI 구하기 - GetResourceURI(assemblyName, resourcePath)

        /// <summary>
        /// 리소스 URI 구하기
        /// </summary>
        /// <param name="assemblyName">어셈블리명</param>
        /// <param name="resourcePath">리소스 경로</param>
        /// <returns>리소스 URI</returns>
        private Uri GetResourceURI(string assemblyName, string resourcePath)
        {
            if(string.IsNullOrEmpty(assemblyName))
            {
                return new Uri(string.Format("pack://application:,,,/{0}", resourcePath));
            }
            else
            {
                return new Uri(string.Format("pack://application:,,,/{0};component/{1}", assemblyName, resourcePath));
            }
        }

        #endregion
        #region 비트맵 이미지 리스트 설정하기 - SetBitmapImageList()

        /// <summary>
        /// 비트맵 이미지 리스트 설정하기
        /// </summary>
        private void SetBitmapImageList()
        {
            this.bitmapImageList.Clear();

            string[] resourcePathArray = new string[]
            {
                "Image/0001.jpg",
                "Image/0002.jpg",
                "Image/0003.jpg",
                "Image/0004.jpg"
            };

            foreach(string resourcePath in resourcePathArray)
            {
                Uri uri = GetResourceURI(null, resourcePath);

                BitmapImage bitmapImage = new BitmapImage(uri);

                this.bitmapImageList.Add(bitmapImage);
            }

            this.currentImage.Source = this.bitmapImageList[this.currentIndex];
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요