Microsoft MVP성태의 닷넷 이야기
WPF에서 로딩중 이미지를 구현 - Source [링크 복사], [링크+제목 복사],
조회: 15809
글쓴 사람
우코아
홈페이지
첨부 파일
 

안녕하세요.
소스코드를 간략하게 추려서 추가로 질문 드립니다.

원하는 것은 이미지가 로딩중에 GIF 이미지를 띄워서 '로딩중..'으로 표현하는 것입니다.
구글링을 뒤져가며 더듬더듬 따라해본 방법은 BackgroundWorker, UI쓰레드, 비동기(Async/Await)등을 해보았습니다.
많은 가르침 부탁 드립니다!


# MainWindow.xaml.cs

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // GIF 이미지를 표현한 Window
            LoadingProcess loadingProcess;
            loadingProcess = new LoadingProcess();
            loadingProcess.Top = this.Top + (this.ActualHeight - loadingProcess.Height) / 2;
            loadingProcess.Left = this.Left + (this.ActualWidth - loadingProcess.Width) / 2;
            loadingProcess.Show();

            //이미지 로딩 호출
            ImageLoading();
        }

        private void ImageLoading()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (ThreadStart)delegate ()
                {
                    int imageWidth = 88;
                    WrapPanel_Images.Children.Clear();
                    DirectoryInfo di = new DirectoryInfo(@"C:\Users\dp\Desktop\TEST");
                    String fileFilters = "*.jpg|*.jpeg|*.png|*.gif|*.tiff|*.bmp";
                    String[] files = fileFilters.Split('|').SelectMany(searchPattern => Directory.GetFiles(@"C:\Users\dp\Desktop\TEST", searchPattern)).ToArray();

                    for (int i = 0; i < files.Length; i++)
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.UriSource = new Uri(@files[i], UriKind.Absolute);
                        bitmapImage.DecodePixelWidth = 10;
                        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                        bitmapImage.EndInit();

                        Image image = new Image();
                        image.Stretch = Stretch.Uniform;
                        image.Width = imageWidth;
                        image.Source = bitmapImage;


                        Border border = new Border();
                        border.BorderThickness = new Thickness(1);
                        border.BorderBrush = new SolidColorBrush(Color.FromRgb(41, 57, 86));
                        border.Margin = new Thickness(2, 2, 2, 2);
                        border.Width = imageWidth;
                        border.Height = imageWidth;
                        border.Child = image;

                        TextBlock imageName = new TextBlock();
                        imageName.Text = System.IO.Path.GetFileName(files[i]);
                        imageName.VerticalAlignment = VerticalAlignment.Center;
                        imageName.HorizontalAlignment = HorizontalAlignment.Center;
                        imageName.Margin = new Thickness(0, 0, 0, 10);
                        imageName.MaxWidth = imageWidth - 4;

                        DockPanel dockPanel = new DockPanel();
                        DockPanel.SetDock(border, Dock.Top);
                        DockPanel.SetDock(imageName, Dock.Bottom);
                        dockPanel.Children.Add(border);
                        dockPanel.Children.Add(imageName);

                        WrapPanel_Images.Children.Add(dockPanel);
                    }
                }
            );
        }
    }
}




# Main.Window.xaml
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DockPanel>
            <Grid DockPanel.Dock="Top">
                <Button Click="Button_Click" x:Name="button1">
                    Click
                </Button>
            </Grid>
            <Grid DockPanel.Dock="Bottom">
                <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                    <WrapPanel x:Name="WrapPanel_Images" Orientation="Horizontal"/>
                </ScrollViewer>
            </Grid>
        </DockPanel>
    </Grid>
</Window>











[최초 등록일: ]
[최종 수정일: 1/4/2019]


비밀번호

댓글 작성자
 



2019-01-04 11시06분
소스 코드가 아니라, 프로젝트를 첨부해 주세요. 아래의 글을 참고하시고.

재현 가능한 최소한의 예제 프로젝트란?
; http://www.sysnet.pe.kr/2/0/11452
정성태

NoWriterDateCnt.TitleFile(s)