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

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

원하는 것은 이미지가 로딩중에 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
정성태

... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1283김영대7/3/201421408안녕하십니까 정성태님 죄송하지만 SmartClient 에 관한 질문이 있습니다. [9]
1282(non...7/2/201418433(글쓴이의 요청으로 삭제합니다.) [2]
1281(non...7/1/201419631(글쓴이의 요청으로 삭제합니다.) [4]
1280동동이6/25/201419071안녕하세요. ocx의 비동기 또는 쓰레드에서 호출 [1]
1279(non...6/23/201419428(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201420584.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201417334silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/201416952쿠키 [1]
1274김솔지6/10/201421424배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201416221정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201426415C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201417007InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/201416498smart client [1]
1266김솔지5/22/201418755clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201423199DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201421650소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201418835159페이지 오타인것 같습니다 [5]
1262(non...5/4/201419703(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201417358다시 한번 질문드릴께요. [2]
1259이근주5/1/201416989도서 오류인 것 같네요.. [1]
1258최세정4/28/201419642안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201418939바인딩 질문입니다. [2]
1251(non...4/20/201423118(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201423335app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201419208(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201418483Http 프로토콜 관련 질문 입니다. [1]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...