2017-02-23 10 views
1

Directory.GetFiles()を使用して作成した画像の文字列配列から画像をpictureBoxにロードしようとしています。私はpicFileを正しく設定することを正しく設定していないと思います。画像の配列から画像をロードしようとしています

以降の画像をロードするためにpictureBox_Clickイベントを作成したよりも、私はしましたが、私はコンソールに文字列配列をリダイレクトする場合は、そのイベントハンドラ

string fileEntries = ""; 

private void showButton_Click(object sender, EventArgs e) 
{ 
    // First I want the user to be able to browse to and select a 
    // folder that the user wants to view pictures in 
    string folderPath = ""; 
    FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); 
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
    { 
     folderPath = folderBrowserDialog1.SelectedPath; 
    } 

    // Now I want to read all of the files of a given type into a 
    // array that from the path provided above 
    ProcessDirectory(folderPath); 

    // after getting the list of path//filenames I want to load the first image here 
    string picFile = fileEntries; 
    pictureBox1.Load(picFile); 
} 

public static void ProcessDirectory(string targetDirectoy) 
{ 
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(targetDirectoy); 
    } 

    // event handler here that advances to the next picture in the list 
    // upon clicking 
} 

を書いていない私は、そのディレクトリ内のファイルのリストを参照してください、それまた、文字列の一部として完全なパスを持っています - それが問題かどうかはわかりません。

+1

pictureBox1.Image = Image.FromFile(picFile)を試してみてください。 –

答えて

1
string[] fileEntries = ProcessDirectory(folderPath); 
if (fileEntries.Length > 0) { 
string picFile = fileEntries[0]; 
pictureBox1.Load(picFile); 
} 

fileEntriesを2回宣言しました。

public static string[] ProcessDirectory(string targetDirectoy) { 
return Directory.GetFiles(targetDirectoy); 
} 
1

今私は

の上方に設けられたパスからだから、あなたが方法ProcessDirectoryの署名を変更する必要があること 配列に指定されたタイプのファイルのすべてを読みたいですすべての画像ファイルを含む文字列affyを返す場合は、検索パターンを使用して特定の拡張子を持つファイルを取得することもできます。私はあなたがすべてのファイルを取得するメソッドを呼び出すことができ、ここで最初の画像

をロードするパス//ファイル名のリストを取得した後

public static string[] ProcessDirectory(string targetDirectoy) 
{ 
    return Directory.GetFiles(targetDirectoy,"*.png"); 
} 

:あなたは、次のシグネチャを使用することができます特定の拡張子を持つ特定のディレクトリそして、配列は任意のファイルを持っている場合のPictureBoxに最初のファイルをロードし、あなたがこのために以下のコードを使用することができます。

var pictureFiles = ProcessDirectory(folderPath); 
if (pictureFiles.Length > 0) 
{ 
// process your operations here   
    pictureBox1.Load(pictureFiles[0]); 
} 
関連する問題