質問編集履歴

1

補足

2017/01/28 18:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -39,3 +39,221 @@
39
39
 
40
40
 
41
41
  素人の質問ですが皆様よろしくお願いします。
42
+
43
+ #####追記
44
+
45
+ 現在、私が書いている特定のフォルダにあるファイル名を配列にするプログラム。
46
+
47
+ ```C#
48
+
49
+ private void Get_Imgs()
50
+
51
+ {
52
+
53
+ string[] files = System.IO.Directory.GetFiles(
54
+
55
+ @"フォルダ名", "*.png", System.IO.SearchOption.AllDirectories);
56
+
57
+ foreach(string file in files)
58
+
59
+ {
60
+
61
+ }
62
+
63
+ }
64
+
65
+ ```
66
+
67
+ 途中でできた、クリックすると画像が変わるプログラム。
68
+
69
+ ```xaml
70
+
71
+ <Window x:Class="画像表示テスト2.MainWindow"
72
+
73
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
74
+
75
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
76
+
77
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
78
+
79
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
80
+
81
+ xmlns:local="clr-namespace:画像表示テスト2"
82
+
83
+ mc:Ignorable="d"
84
+
85
+ Title="MainWindow"
86
+
87
+ Background="Black">
88
+
89
+ <Grid>
90
+
91
+ <Image Name="img" Stretch="Uniform"/>
92
+
93
+ </Grid>
94
+
95
+ </Window>
96
+
97
+ ```
98
+
99
+ ```C#
100
+
101
+ using System;
102
+
103
+ using System.Collections.Generic;
104
+
105
+ using System.Linq;
106
+
107
+ using System.Text;
108
+
109
+ using System.Threading.Tasks;
110
+
111
+ using System.Windows;
112
+
113
+ using System.Windows.Controls;
114
+
115
+ using System.Windows.Data;
116
+
117
+ using System.Windows.Documents;
118
+
119
+ using System.Windows.Input;
120
+
121
+ using System.Windows.Media;
122
+
123
+ using System.Windows.Media.Imaging;
124
+
125
+ using System.Windows.Navigation;
126
+
127
+ using System.Windows.Shapes;
128
+
129
+ using System.IO;
130
+
131
+ using System.Drawing;
132
+
133
+
134
+
135
+
136
+
137
+ namespace 画像表示テスト2
138
+
139
+ {
140
+
141
+ /// <summary>
142
+
143
+ /// MainWindow.xaml の相互作用ロジック
144
+
145
+ /// </summary>
146
+
147
+ public partial class MainWindow : Window
148
+
149
+ {
150
+
151
+ public MainWindow()
152
+
153
+ {
154
+
155
+ InitializeComponent();
156
+
157
+ this.get_image();
158
+
159
+ }
160
+
161
+ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
162
+
163
+ {
164
+
165
+ base.OnMouseLeftButtonDown(e);
166
+
167
+ this.Show_Image();
168
+
169
+ count++;
170
+
171
+ }
172
+
173
+ protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
174
+
175
+ {
176
+
177
+ base.OnMouseLeftButtonDown(e);
178
+
179
+ this.Show_Image();
180
+
181
+ count--;
182
+
183
+ }
184
+
185
+ public string[] files;
186
+
187
+ public int count = 0;
188
+
189
+ private void get_image()
190
+
191
+ {
192
+
193
+ files = System.IO.Directory.GetFiles(
194
+
195
+ @"フォルダ名", "*.png", System.IO.SearchOption.AllDirectories);
196
+
197
+ }
198
+
199
+ private void Show_Image()
200
+
201
+ {
202
+
203
+ if(count < 0)
204
+
205
+ {
206
+
207
+ count = files.Length - 1;
208
+
209
+ }
210
+
211
+ if(count > files.Length - 1)
212
+
213
+ {
214
+
215
+ count = 0;
216
+
217
+ }
218
+
219
+ var image = new BitmapImage();
220
+
221
+ image.BeginInit();
222
+
223
+ image.UriSource = new Uri(files[count], UriKind.RelativeOrAbsolute);
224
+
225
+ image.CacheOption = BitmapCacheOption.OnLoad;
226
+
227
+ image.EndInit();
228
+
229
+ img.Source = image;
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+ ```
238
+
239
+ 普段は
240
+
241
+ BitmapImage img = new BitmapImage(new Uri("ファイル名", UriKind.Relative))
242
+
243
+ this.image1.Source = img;
244
+
245
+ という風に画像を切り替えています。今回はこの方法ではウィンドウに画像が表示されなかったので
246
+
247
+ こちら(「[WPFアプリケーションで画面に画像を表示するサンプルプログラム](https://code.msdn.microsoft.com/WPF-b1f4d147/sourcecode?fileId=19117&pathId=949906391)」)のサンプルに
248
+
249
+ 書かれていたことを少し変えて、画像が表示されるようにしました。
250
+
251
+ この方法で画像を読み込んでいると、imageArrayに入れられないようです。
252
+
253
+ このままでは、「やりたいことだけ言って丸投げ人間」になってしまうと思って自分なりに調べてみたのですが
254
+
255
+ System.Windows.Controls.Imageで画像を読み込む方法が分かりません。
256
+
257
+ もし可能であれば、どういったキーワードで調べればよいか教えていただけると
258
+
259
+ 助かります。