스레드 사용한 이미지 뷰어 프로그램 - 데모.
WPF 2007. 9. 12. 18:38 |PathInfo.cs
----------------------------
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace GsiImageView
{
public class PathInfo
{
private string _path;
private string _filename;
public string Path
{
get { return _path; }
set { _path = value; }
}
public string FileName
{
get { return _filename; }
set { _filename = value; }
}
public PathInfo(string path, string filename)
{
this._path = path;
this._filename = filename;
}
}
public class PathInfoList : ObservableCollection<PathInfo>
{
public PathInfoList()
{
//최초 입력
Add(new PathInfo(@"D:\WPF_Project\GsiImageView\Image\1111.jpg", "1111.jpg"));
Add(new PathInfo(@"D:\WPF_Project\GsiImageView\Image\2222.jpg", "2222.jpg"));
}
}
}
Window1.xaml
-------------------------------
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:GsiImageView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:GsiImageView="clr-namespace:GsiImageView"
x:Class="GsiImageView.Window1"
x:Name="Window"
Title="Gsi ImageViewer"
Width="640" Height="480">
<Window.Resources>
<c:PathInfoList x:Key="pathData" />
<ObjectDataProvider x:Key="PathInfoListDS" d:IsDataSource="True" ObjectType="{x:Type GsiImageView:PathInfoList}"/>
<DataTemplate x:Key="PathInfoListTemplate">
<DataTemplate.Resources>
<Storyboard x:Key="OnLoaded1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="stackPanel" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<StackPanel HorizontalAlignment="Stretch" Margin="0,2,2,2" x:Name="stackPanel" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5">
<StackPanel.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFA1A1A1" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</StackPanel.RenderTransform>
<TextBlock HorizontalAlignment="Stretch" Margin="0,2,0,2" VerticalAlignment="Stretch" Padding="4,0,0,0" Text="{Binding Path=FileName}"/>
<Border Height="Auto" BorderBrush="#FFCFD6FF" BorderThickness="2,2,2,2" Width="{Binding ElementName=Slider_ImageSize, Path=Value}">
<Image Width="Auto" Height="Auto" Source="{Binding Path=Path}"/>
</Border>
</StackPanel>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="stackPanel">
<BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<WrapPanel/>
</ItemsPanelTemplate>
</Window.Resources>
<Window.Background>
<LinearGradientBrush EndPoint="0.988,0.988" StartPoint="0.008,0.054">
<GradientStop Color="#FFB5B5B5" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Window.Background>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="30.192"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label HorizontalContentAlignment="Center" Padding="0,0,0,0" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="0,0,0,0" Width="43" Content="경로 :"/>
<TextBox Background="{x:Null}" BorderBrush="#7F939393" BorderThickness="2,2,2,2" HorizontalContentAlignment="Left" Padding="0,1,0,1" VerticalContentAlignment="Center" Margin="43,0,0,0" x:Name="DirBox" Text="TextBox" TextWrapping="Wrap" HorizontalAlignment="Left" Width="59"/>
<Button HorizontalAlignment="Right" Margin="0,0,0,0" x:Name="FolderOpen" Width="63" Content="테스트" Click="OnFolderOpenClick"/>
<ListBox IsSynchronizedWithCurrentItem="True" Background="{x:Null}" Margin="4,4,4,4" x:Name="ImgListBox" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemTemplate="{DynamicResource PathInfoListTemplate}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" ItemsSource="{Binding Mode=OneWay, Source={StaticResource pathData}}"/>
<Slider Margin="109,0,70,0" x:Name="Slider_ImageSize" VerticalAlignment="Center" Maximum="1024" Minimum="10" Value="100"/>
</Grid>
</Window>
Window1.xaml.cs
-----------------------------
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Threading;
namespace GsiImageView
{
public partial class Window1
{
public delegate void UpdateImage(string _path);
public Window1()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
//ObjectDataProvider dp = (ObjectDataProvider)this.FindResource("PathInfoListDS");
//pathInfolist = (PathInfoList)dp.Data;
}
private void OnFolderOpenClick(object sender, RoutedEventArgs e)
{
//ImgListBox.Items.Add(new PathInfo(@"D:\WPF_Project\GsiImageView\Image\3333.jpg", "3333.jpg"));
//Window1 w1 = new Window1();
Thread loadThread = new Thread(new ThreadStart(DataLoad));
loadThread.Start();
Thread.Sleep(1);
}
private void DataLoad()
{
//디렉토리 경로 가져 오기
DirectoryInfo di = new DirectoryInfo(@"D:\WPF_Project\GsiImageView\Image\MaxImageTest");
try
{
if (di.Exists)
{
//경로가 있음
foreach (FileInfo fi in di.GetFiles())
{
//경로를 타고 세부 이미지 이름을 얻어 온다.
string path = di.FullName + "\\" + fi.Name;
//il.Add(new PathInfo(path, fi.Name));
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.DataBind, new UpdateImage(UpdateImageList), path);
Thread.Sleep(60);
}
}
else
{
MessageBox.Show("경로가 확실하지 않습니다.");
return;
}
}
catch
{
MessageBox.Show("경로가 확실하지 않거나, 비정상 오류 입니다.");
return;
}
}
public void UpdateImageList(string _path)
{
PathInfoList pathinfolist = (PathInfoList)this.FindResource("pathData");
pathinfolist.Add(new PathInfo(_path, "none.jpg"));
// Thread.Sleep(1);
/*
//pathInfolist.Add(new PathInfo(_path, "none"));
ImgListBox.Items.Add(new PathInfo(_path, "none"));
ImgListBox.UpdateLayout();
try
{
int iii = 0;
}
catch(Exception e)
{
int iii = 0;
}
*/
}
}
}