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

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

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

정성태

... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
4936무개1/3/20188787책 내용 인용하는 것에 관한 질문이있습니다. [1]파일 다운로드1
4935몬난아12/28/201711528IIS 및 ASP.NET 이 서버에서의 동작방식? [2]
4934Ques...12/26/201721888C# 해상도에 따른 자동 사이즈 조절방법이 궁금합니다. [3]
4933Kay12/15/20179207C# VS 버전 에러 질문 드립니다. [1]파일 다운로드1
4932정환나라12/13/20179115vs2013에서 빌드한 com 컨트롤 객체를 닷넷 2.0에서 사용하려 합니다 [4]
4931Arvid12/12/201710154Visual Studio 2012 c# using문 참조 에러 [5]파일 다운로드1
4929김성대12/8/20178639app.config에 관한질문입니다. [1]
4928김성대12/7/20179309SQL Server 설치에러에 대한 질문입니다. [1]파일 다운로드1
4926heyh...12/6/20178544[삭제] ContextSwitchDeadlock????
4925ho12/5/20179079[삭제] WebBrowser로 드롭박스 로그인 페이지 탐색 시 발생한 문제에 대해 질문 올립니다.파일 다운로드2
4924몽중언12/3/20179524C# 디버깅 모드에서만 DB Insert가 되는 현상 질의 [6]
4923고요한11/23/20179645파일 확장자에 연결된 프로그램을 등록하는 방법에 대한 질문입니다. [2]
4922박성훈11/23/201710597시작하세요! C# 7.1 학습 방법 [3]
4921초보개발자11/20/20177838[삭제] 폼 사이즈 질문드립니다.
4920Ques...11/19/20178953IEnumerable 의 "지연된 평가" 에 관하여 질문드립니다. [2]파일 다운로드1
4919mskim11/16/20179254Split()을 이용하여 문자 구분 시 구분문자도 같이 저장하는 방법이 있나요? [2]
4917ho11/16/20179876WPF XAML 트리거 관련해 문의 드립니다. [3]
4918ho11/16/201710183    답변글 [답변]: 예제 프로젝트 첨부합니다. [4]파일 다운로드2
4916필승11/11/20179835기본 웹 브라우저 체크는 어떻게 해야 하나요? [2]
4915필승11/10/20178914WebBrowser 컨트롤 소리 출력 문의 드립니다. [2]
4914Ques...11/10/20179587c# 버튼 이벤트에 관하여 질문드립니다. [2]
4913Arvid11/8/201712009Log4Net 라이브러리를 이용하여 특정 기간이 지났을 때 자동 로그 삭제 기능 구현 질문 드립니다! [2]
4912조범희11/8/201711688C# FTP 다운로드중 에러 발생.. [2]파일 다운로드1
4911필승11/6/201710126WebBrowser 컨트롤 사용법에 대해 궁금합니다. [2]
4910진우11/6/20179696람다 초보 질문 드립니다. [2]
4909필승11/5/201710628TextBox에 관해 질문 드립니다. [5]
... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...