2013-08-10 21 views
5

私はユーザーのパーミル編集ウィンドウを作成しようとしていますが、このウィンドウには画像コントロールがあります
イメージファイルを選択すると、最初の時間は大丈夫ですが、2回目はエラーが表示されますイメージファイルのコピーが別のプロセスで使用されています

"別のプロセスで使用されているため、ファイル 'C:\ 1.jpg'にアクセスできません。

は、私はあなたがロードし、画像を表示し、従順なファイルを保存したい場合は、私は

private void Select_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog od = new OpenFileDialog(); 
    if (od.ShowDialog() == true) 
    { 
     string imageLocal = @"C:/1.jpg"; 
     File.Copy(od.FileName, imageLocal, true); 
     image1.Source = new BitmapImage(new Uri(imageLocal)); 
    } 
} 
+0

問題ここでは、ファイルをロードした画像に変換する方法です。使用しているコンストラクタメソッドを使用しても機能しません。別のプロセスで使用される例外を避けたい場合は、別の方法があります。 –

+0

この問題を解決するにはどうすればよいですか? – Lai32290

答えて

5

を行うことができますかわからない、私のImageコントロールは、このファイルを使用しているので、それはあると思いますので、ファイルシステムの操作(リロードや別のディレクトリへの移動など)では、(あなたが指摘したように)BitmapImageクラスがファイルハンドルにハングするため、Uriコンストラクタが機能しません。

代わりに、このような方法を使用して...

private static BitmapImage ByStream(FileInfo info) 
    { //http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dee7cb68-aca3-402b-b159-2de933f933f1 
     try 
     { 
      if (info.Exists) 
      { 
       // do this so that the image file can be moved in the file system 
       BitmapImage result = new BitmapImage(); 
       // Create new BitmapImage 
       Stream stream = new MemoryStream(); // Create new MemoryStream 
       Bitmap bitmap = new Bitmap(info.FullName); 
       // Create new Bitmap (System.Drawing.Bitmap) from the existing image file 
              (albumArtSource set to its path name) 
       bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
       // Save the loaded Bitmap into the MemoryStream - Png format was the only one I 
           tried that didn't cause an error (tried Jpg, Bmp, MemoryBmp) 
       bitmap.Dispose(); // Dispose bitmap so it releases the source image file 
       result.BeginInit(); // Begin the BitmapImage's initialisation 
       result.StreamSource = stream; 
       // Set the BitmapImage's StreamSource to the MemoryStream containing the image 
       result.EndInit(); // End the BitmapImage's initialisation 
       return result; // Finally, set the WPF Image component's source to the 
           BitmapImage 
      } 
      return null; 
     } 
     catch 
     { 
      return null; 
     } 
    } 

この方法では、FileInfoオブジェクトを受け取り、あなたが表示し、同時に別のディレクトリに移動するか、再度それを表示することができBitmapImageのを返します。

public static BitmapImage LoadBitmapImage(string fileName) 
{ 
    using (var stream = new FileStream(fileName, FileMode.Open)) 
    { 
     var bitmapImage = new BitmapImage(); 
     bitmapImage.BeginInit(); 
     bitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
     bitmapImage.StreamSource = stream; 
     bitmapImage.EndInit(); 
     bitmapImage.Freeze(); 
     return bitmapImage; 
    } 
} 
+0

オープンJPGファイルを試していますが、この形式のコードを編集しました。 bitmap.Save(stream、ImageFormat.Jpeg); 。しかし、FileFormatExceptionが実行されます。 msgstr "画像をデコードできません。画像ヘッダが壊れている可能性があります。" – Lai32290

+0

破損していないファイルで試してください –

+0

はい、ImageFormat.PNGとPNGファイルを試しても問題はなく、JPEGを使用すると問題があります。 – Lai32290

2

以下に示す方法は、ファイルからのBitmapImageをロードし、すぐにロードした後、ファイルを閉じます。

Aはるかに簡単な方法は、下記の別の答えからコピーされ、これです。ソースストリームがEndInitの直後に閉じているときには、BitmapCacheOption.OnLoadフラグを設定する必要があることに注意してください。

public static BitmapImage LoadBitmapImage(string fileName) 
{ 
    using (var stream = new FileStream(fileName, FileMode.Open)) 
    { 
     var bitmapImage = new BitmapImage(); 
     bitmapImage.BeginInit(); 
     bitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
     bitmapImage.StreamSource = stream; 
     bitmapImage.EndInit(); 
     bitmapImage.Freeze(); // just in case you want to load the image in another thread 
     return bitmapImage; 
    } 
} 

このコードは、WPFでサポートされているすべての画像形式で使用できます。ストリームとして画像ファイルのコンテンツをStreamSourceプロパティに渡すと、WPFは自動的に適切なデコーダを作成します。

1

非常にシンプルなソリューション:

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 

File.Copy(od.FileName, imageLocal, true); 
関連する問題