앱개발
wpf - image loading
제갈티
2025. 6. 16. 20:46
<Application x:Class="wpf_LoadImage.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_LoadImage"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
- App.xaml
<Window x:Class="ImageViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Image Viewer" Height="600" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Name="LoadImageButton" Content="이미지 불러오기"
Margin="10" Padding="10,5" Click="LoadImageButton_Click"/>
<Image Grid.Row="1" Name="DisplayImage" Stretch="Uniform"
Margin="10"/>
</Grid>
</Window>
- MainWindow.xaml
using Microsoft.Win32;
using System.Windows;
using System.Windows.Media.Imaging;
namespace ImageViewer
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void LoadImageButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "이미지 파일|*.jpg;*.jpeg;*.png;*.bmp;*.gif|모든 파일|*.*";
openFileDialog.Title = "이미지 파일 선택";
if (openFileDialog.ShowDialog() == true)
{
try
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new System.Uri(openFileDialog.FileName);
bitmap.EndInit();
DisplayImage.Source = bitmap;
}
catch (System.Exception ex)
{
MessageBox.Show($"이미지를 불러오는 중 오류가 발생했습니다: {ex.Message}",
"오류", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}
- MainWindow.xaml.cs