動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
pngなどの画像を表示するには。
参考
WPF での画像読み込みをバックグラウンドで処理する by ちとくさん
情報感謝です。
code
上記のリンクのうち、Image.Sourceを指定する方法を使ってみる。
MainWinow.xaml
<Window x:Class="_171115_t1200_imageLoad.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:_171115_t1200_imageLoad"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Name="B_loadImage" Content="load image" Height="30" Click="B_loadImage_Click"/>
<Image Name="IMG_deltaFlyer" Width="400" Height="270"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _171115_t1200_imageLoad
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void B_loadImage_Click(object sender, RoutedEventArgs e)
{
// UIスレッドで行う例
var source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("file://D:\\Image_171115\\deltaFlyer.png");
source.EndInit();
IMG_deltaFlyer.Source = source;
}
}
}
deltaFlyer.pngを用意してボタンを押してみた。
備考
画像の表示に関しては他にもいろいろ方法はありそうだ。
必要になった時点で他の方法を使ってみる。