2016-11-19 9 views
0

アップローダが画像を切り取るたびに新しい画像を保存しています。新しい画像があればチェックしたいと思います。ように..私はそれがimage3cropped image2cropped、のように保存したいディレクトリに複数のファイルが存在するかどうか確認してください

private void pictureBox5_MouseUp(object sender, MouseEventArgs e) 
     { 
      Selecting = false; 

      // Copy the selected area. 
      SelectedArea = GetSelectedArea(pictureBox5.Image, Color.Transparent, Points); 

      SelectedArea.Save(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png); 


     } 

、それは

if(File.Exists(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png); 

のように存在しているかどうかを確認、私はそれがimage2cropped、image3croppedのようにチェックしたい...と:私はこのコードを持っています。 アイデア?

答えて

0

私が理解しているのは、ファイルに領域を保存していることです。初めて、image1cropped.pngに保存した後、最初のファイルを上書きしないためにimage2croppedを自動で使用しますか? この場合、次のコードを使用できます。

 int i = 0; // set 0 to start at 1 for "image1cropped" 
     string freeFileName; // the final fileName that doesn't exist 

     // loop while file imageXcropped exists 
     do 
     { 
      i++; 
      freeFileName = @"C:\Users\User\Desktop\Gallery\image" + i + "cropped.png"; 
     } while (File.Exists(freeFileName)); 

     // at this point freeFileName doesn't exists, you can use it 
     // use : SelectedArea.Save(freeFileName, ImageFormat.Png); 
+0

はい私はそれを解決しました。しかし、私は彼らが同じアルゴリズムであるかどうかをチェックしたいですか? –

+1

私は決してそれを解決しました。ここでは は最終コードです。 SelectedArea.Save(@ "C:\ Users \ User \ Desktop \ Gallery \ image" + NumberOfClick.ToString()+ "cropped.png"、ImageFormat.Png); 文字列ファイル名= @ "C:¥Users¥User¥Desktop¥Gallery¥image" + NumberOfClick.ToString()+ "cropped.png"; if(File.Exists(filename)) { button1.Visible = true; pictureBox5.Visible = false; } –

+0

ファイルを上書きしないで保存する前にチェックしますか、または保存後にファイルが正常に保存されていることを確認しますか? – Elloco

関連する問題