2013-08-10 4 views
5

사용자 권한 만들기를 시도하고 있습니다.이 창에서 이미지 컨트롤을 가지고 있습니다.
이미지 파일을 선택하면이 이미지 컨트롤에이 파일이 복사됩니다 내 이미지 폴더에 파일이 처음 있지만 괜찮습니다. 두 번째로 오류가 표시됩니다.이미지 파일 복사본을 다른 프로세스에서 사용 중입니다.

"다른 프로세스에서 사용 중이므로 '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

을 무엇을 할 수 있는지 모르겠다, 내 이미지 컨트롤이 파일을 사용하고 있기 때문에 생각하는, 그래서 (다시로드하거나 다른 디렉토리로 이동하는 것과 같은) 파일 시스템의 작업에 대해서는 Uri 생성자가 작동하지 않습니다 (지적한대로) BitmapImage 클래스가 파일 핸들에 정지합니다.

대신,

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이 실행됩니다. "이미지를 디코딩 할 수 없습니다. 이미지 헤더가 손상되었을 수 있습니다." – Lai32290

+0

손상되지 않은 파일로 시도하십시오 –

+0

예, ImageFormat.PNG 및 PNG 파일을 시도했을 때 문제가 없었으며 모든 JPEG 또는 JPG 파일에 대해 JPEG을 사용할 때 문제가 있음 – Lai32290

2

아래 방법은 파일에서 BitmapImage를로드하고 즉시 로딩 후 파일을 닫습니다 :

훨씬 간단한 방법은, 아래의 다른 답변에서 복사, 이것이다. 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); 
관련 문제