-2

重複の可能性:
WPF アプリケーションからアプリケーションのディレクトリを取得する

「C:\ Path」を使用せずにJavaのようにプロジェクトディレクトリからファイルにアクセスしたいのは、画像ボックスにファイル例外を作成するためです。これはタイマーのコードです

if (imagecount == 30)
{
    this.pictureBox1.Image = System.Drawing.Image.FromFile(@"C:\Users\Baloi\Documents\visual studio 2010\Projects\WinEX\WinEX\" + image() + ".jpg");
    imagecount = 0;
}

else if (imagecount < 30)
    imagecount++;
4

3 に答える 3

2

アプリケーション ディレクトリ:

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

実行可能ディレクトリ:

string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath);

要件に基づいて、上記のいずれかをPath.Combineで使用して、イメージの場所へのフル パスを構築できます。

または、リソース ファイルに画像を埋め込むこともできます。次に、それらを次のようにロードできます

Stream imgStream = 
    Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);
于 2012-06-03T18:17:43.463 に答える
0

次のコードを使用できます。

this.pictureBox1.Image = System.Drawing.Image.FromFile(image() + ".jpg");

ファイルは、プログラムと同じフォルダーにある必要があります。

于 2012-06-03T18:36:10.713 に答える
0

いくつかのオプションがあります:

  1. プロジェクトに画像を埋め込みます (コンパイル アクションを埋め込みデータに設定します)。

  2. 相対パスを使用して写真を参照します。これは、デバッグ中にバイナリ アセンブリが bin\Debug フォルダーにあるという事実によって、少し複雑になります。

オプション 1 の場合:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);

http://msdn.microsoft.com/en-us/library/aa287676(v=vs.71).aspx

オプション 2 の場合:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
if (System.Diagnostics.Debugger.IsAttached)
{
    contentDirectory = Path.Combine(appPath + @"\..\..\content");
}
else
{   
    contentDirectory = Path.Combine(appPath, @"content");
}
于 2012-06-03T18:41:57.587 に答える