LoginSignup
3
1

More than 5 years have passed since last update.

Visual Studio | WPF > 画像 > png画像を表示する > BitmapImage()と.Source指定

Posted at
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

pngなどの画像を表示するには。

参考

WPF での画像読み込みをバックグラウンドで処理する by ちとくさん

情報感謝です。

Image.Sourceプロパティ

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を用意してボタンを押してみた。

qiita.png

備考

画像の表示に関しては他にもいろいろ方法はありそうだ。

必要になった時点で他の方法を使ってみる。

WPFでの印刷の基本(3) 図形や文字の印字

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1