2017-07-05 20 views
0

イメージをコピーしてGUIDで名前を変更しても問題は発生しませんでした。 しかし、私はピクチャボックスに、たとえば、このイメージを開いてみたかったとき、私はこれだ:私はこのファイルを開くことができますどのように@"images\full_45e72053-440f-4f20-863c-3d80ef96876f.jpeg"パスに不正な文字を含むファイルを開く

:debugerでパスを持つ enter image description here

生成された名前は次のようになりますか?

これは私にこの問題を示して私のコードです:

private void picBoxMini2_Click(object sender, EventArgs e) 
     { 
      string dir = ConfigurationManager.AppSettings["imageFolderPath"].ToString(); 
      string imgName = this.picBoxMini2.ImageLocation; 
      string[] tmp = imgName.Split('_'); 
      this.picBoxMain.Image = Image.FromFile($"{dir}\full_{tmp[tmp.Length - 1]}"); 
     } 

ImageLocationは、私がこのような状況に被保険者、100%の情報が含まれています

string dir = ConfigurationManager.AppSettings["imageFolderPath"].ToString(); 
      if (imgs.Length >= 1) 
      { 
       this.picBoxMain.Image = Image.FromFile([email protected]"{dir}\full_{imgs[0]}"); 
       this.picBoxMain.ImageLocation = [email protected]"{dir}\full_{imgs[0]}"; 
       this.picBoxMini1.Image = Image.FromFile([email protected]"{dir}\85_{imgs[0]}"); 
       this.picBoxMini1.ImageLocation = [email protected]"{dir}\85_{imgs[0]}"; 

       this.picBoxMini2.Image = null; 
       this.picBoxMini2.ImageLocation = null; 
       this.picBoxMini3.Image = null; 
       this.picBoxMini3.ImageLocation = null; 
      } 
      if (imgs.Length >= 2) 
      { 
       this.picBoxMini2.Image = Image.FromFile([email protected]"{dir}\85_{imgs[1]}"); 
       this.picBoxMini2.ImageLocation = [email protected]"{dir}\85_{imgs[1]}"; 
      } 
      if (imgs.Length == 3) 
      { 
       this.picBoxMini3.Image = Image.FromFile([email protected]"{dir}\85_{imgs[2]}"); 
       this.picBoxMini3.ImageLocation = [email protected]"{dir}\85_{imgs[2]}"; 
      } 
+0

最初のコードブロックの '.FromFile'呼び出しで' @ 'を忘れたと思います。 –

+0

@BradleyUffnerはいそれはあります!私はとても疲れていて、このシンボルを見逃していました。今、すべて完璧に動作します! THX – NemoUA

答えて

1

問題は、この行にある:

this.picBoxMain.Image = Image.FromFile($"{dir}\full_{tmp[tmp.Length - 1]}"); 

@を忘れてしまった場合は、コンパイラに文字列を逐語的に扱うように指示します。このマークがなければ、あなたのパスには、Windowsのファイル名の正当な文字ではない、埋め込まれたctrl + f文字(\ fの\full)があると考えられます。

あなたのオプションは次のとおりです。

  • @を含める:this.picBoxMain.Image = Image.FromFile([email protected]"{dir}\full_{tmp[tmp.Length - 1]}")
  • ディレクトリの区切り文字をエスケープ:this.picBoxMain.Image = Image.FromFile($"{dir}\\full_{tmp[tmp.Length - 1]}")
  • は自動的にディレクトリ/ファイル名の区切りを処理するためにSystem.IO.Path.Combineで何か他のもの空想を行います。 this.picBoxMain.Image = Image.FromFile(System.IO.Path.Combine(dir, $"full_{tmp[tmp.Length - 1]}"))(これは恐らく最も安全な、最もポータブルな解決策ですが、あなたのニーズには余計かもしれません)
関連する問題