皆様こんにちは。いつもお世話になっております。
質問させていただきます。
現在、私はWPFを使用してデスクトップマスコットを作っています。
参考にしたのは、こちらのサイト(「C#/WPFで作るデスクトップマスコット入門」)です。
ふと、特定のフォルダの中にある画像を一括でウィンドウに表示できたら
フォルダの中身を変えるだけで見た目を変更できてよいのでは?と思ったのですが
フォルダの中身を一括でウィンドウに表示する方法が分かりません。
WPFの何を利用すればこれが可能なのかヒントをいただけると幸いです。
具体的には、フォルダのファイルを全て取得した後、配列にして
その配列をウィンドウに表示するみたいなことが可能なのでしょうか?
素人の質問ですが皆様よろしくお願いします。
#####追記
現在、私が書いている特定のフォルダにあるファイル名を配列にするプログラム。
C#
1 private void Get_Imgs() 2 { 3 string[] files = System.IO.Directory.GetFiles( 4 @"フォルダ名", "*.png", System.IO.SearchOption.AllDirectories); 5 foreach(string file in files) 6 { 7 } 8 }
途中でできた、クリックすると画像が変わるプログラム。
xaml
1<Window x:Class="画像表示テスト2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:画像表示テスト2" 7 mc:Ignorable="d" 8 Title="MainWindow" 9 Background="Black"> 10 <Grid> 11 <Image Name="img" Stretch="Uniform"/> 12 </Grid> 13</Window>
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15using System.IO; 16using System.Drawing; 17 18 19namespace 画像表示テスト2 20{ 21 /// <summary> 22 /// MainWindow.xaml の相互作用ロジック 23 /// </summary> 24 public partial class MainWindow : Window 25 { 26 public MainWindow() 27 { 28 InitializeComponent(); 29 this.get_image(); 30 } 31 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 32 { 33 base.OnMouseLeftButtonDown(e); 34 this.Show_Image(); 35 count++; 36 } 37 protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) 38 { 39 base.OnMouseLeftButtonDown(e); 40 this.Show_Image(); 41 count--; 42 } 43 public string[] files; 44 public int count = 0; 45 private void get_image() 46 { 47 files = System.IO.Directory.GetFiles( 48 @"フォルダ名", "*.png", System.IO.SearchOption.AllDirectories); 49 } 50 private void Show_Image() 51 { 52 if(count < 0) 53 { 54 count = files.Length - 1; 55 } 56 if(count > files.Length - 1) 57 { 58 count = 0; 59 } 60 var image = new BitmapImage(); 61 image.BeginInit(); 62 image.UriSource = new Uri(files[count], UriKind.RelativeOrAbsolute); 63 image.CacheOption = BitmapCacheOption.OnLoad; 64 image.EndInit(); 65 img.Source = image; 66 } 67 } 68}
普段は
BitmapImage img = new BitmapImage(new Uri("ファイル名", UriKind.Relative))
this.image1.Source = img;
という風に画像を切り替えています。今回はこの方法ではウィンドウに画像が表示されなかったので
こちら(「WPFアプリケーションで画面に画像を表示するサンプルプログラム」)のサンプルに
書かれていたことを少し変えて、画像が表示されるようにしました。
この方法で画像を読み込んでいると、imageArrayに入れられないようです。
このままでは、「やりたいことだけ言って丸投げ人間」になってしまうと思って自分なりに調べてみたのですが
System.Windows.Controls.Imageで画像を読み込む方法が分かりません。
もし可能であれば、どういったキーワードで調べればよいか教えていただけると
助かります。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/01/28 16:36