2016-05-15 8 views
0

私はOpenFileDialogを使用してイメージのパスを取得し、それをWPFアプリケーションのイメージソースプロパティimgSelected.Source = new BitmapImage(new Uri(filenameSteg, UriKind.Absolute));に設定します。C#OpenFileDialogファイルを破棄する方法

事があり、私は後でもう一度そのファイルを開く必要がありますが、ファイルが別のプロセス(System.IO.IOException - >The process cannot access the filepathToFilebecause it is being used by another process.)で使用されてあるので、私はそれを開くことができません。次のように後でそれにアクセスする必要がある

コード:

bitmapSource = JpegBitmapDecoder.Create(File.Open(filenameSteg, FileMode.Open), 
       BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0]; 

このbitmapSourceWriteableBitmapにそのイメージを与えるために使用され、そこから私はピクセルを通過します。

ファイルをOpenFileDialogで開いた方法はありますか?

私はIDisposableにキャストしようとしましたが、ブロックを使用していましたが、これは永続的です。

EDIT:

1 - 私はこの(@ctacke答え)試してみました:

using (var stream = File.Open(filenameSteg, FileMode.Open)){ 
     bitmapSource = JpegBitmapDecoder.Create(stream, BitmapCreateOptions.None, 
     BitmapCacheOption.OnLoad).Frames[0];} 

をしかし、理由もかかわらず、それはまだ私に別のプロセスで使用中で、既にされているプロセスに関するエラーを与えます私はまだで開いたものと同じファイル(filenameSteg)を開こうとしています。 (あるいは、少なくとも、それは私がそれを見る方法です。)

2 - 私はこれを試してみました(@ctackeに基づいてlinkをお勧めします:

using (FileStream fileStream = new FileStream(filenameSteg+1, FileMode.Create)){ 
     MemoryStream memoryStream = new MemoryStream(); 
     BitmapImage bi = new BitmapImage(); 
     byte[] fileBytes = File.ReadAllBytes(filenameSteg); 
     memoryStream.Write(fileBytes, 0, fileBytes.Length); 
     memoryStream.Position = 0; 

     bi.BeginInit(); 
     bi.StreamSource = memoryStream; 
     bi.EndInit(); 

     bitmapSource = bi;} 

注:私はここにfilenameSteg +1を求めるていることに注意してください。私は残りのメソッドをテストしたいので、私はファイルをコピーして、単にその名前に1を加えました。つまり、実際にfilenameStegを使用すると、私はすでに使用中であることについて私に同じエラーを与えました。再び疑問に思うのは、以前に開かれた同じイメージを開こうと依頼しているということです。OpenFileDialog

3 - 私が開いて画像を配置するために私を必要としない別のアプローチを考えた:

私はOpenFileDialogで初めて画像を開くと、私はそう変数に画像のバイト配列を格納しますBitmapFactoryとバイト配列を使用してWriteableBitmapを作成することができました。

// This is in the OpenFileDialog. It is where I stock the image "pixels" in a byte array. 
bytesArrayImage = File.ReadAllBytes(filenameSteg); 
//And then later when I needed the WriteableBitmap, I used the byte array and the BitmapFactory 
//imgSelected being the Image containing the image I opened in the OpenFileDialog, I used it's 
//width and height 
wb = BitmapFactory.New(Convert.ToInt32(imgSelected.Width), 
    Convert.ToInt32(imgSelected.Height)).FromByteArray(bytesArrayImage); 

このアプローチの問題点は、いくつかの写真は正常に動作し、私はWriteableBitmapを作成するために、バイト配列を使用して、それの画素を通過することができますが、他の例では、それは私に知らせるAccessViolationExceptionを与える、ということである:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.。言い換えれば、処分問題を回避しようとすると別の問題に陥りました。

答えて

0

あなたは、このような元の画像を解放する必要があります:あなたが明示的に解放する必要があるストリームを返しFile.Open

if(imgSelected.Source != null) 
{ 
    imgSelected.Source.Dispose; 
} 
imgSelected.Source = new BitmapImage(new Uri(filenameSteg, UriKind.Absolute)); 

次へ]を、。

using(var stream = File.Open(filenameSteg, FileMode.Open)) 
{ 
    var bitmapSource = JpegBitmapDecoder.Create(stream, 
     BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0]; 
} 

参照:高速な応答のための@ctacke Load a BitmapSource and save using the same name in WPF -> IOException

+0

おかげで、私はあなたが私を呼ばれる方法の両方で私の質​​問を編集した(ストリームを取得するために使用してブロックを使用し、それが配置されます)あなたが情報として与えたリンク内のもの。私はそれをバイパスしようとした後、私が現在いるところに現在の状態を置く。 – Marks

関連する問題