2013-12-23 2 views
3

OK ...이 코드의 일부 가장 유용합니다. 리소스를 해제해야합니다. 그렇지 않으면 파일을 webclient 또는 기타에서 사용할 수 없습니다. 기타 :webclient가 파일을 사용할 수 없습니다. (다른 프로세스에서 사용 중입니다.)

WebClient webClient = new WebClient(); 
string remote = "sample.jpg"; 
string px = Request.PhysicalApplicationPath.ToString(); 
if (File.Exists(px+"1.jpg") != true) 
{ 
    string local = px + "1.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
else 
{ 
    string local = px + "2.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
try 
{ 
    byte A, R, G, B; 
    Color pixelColor; 
    Color pixelColor1; 

    string rt = px + "1.jpg"; 
    string rt1 = px + "2.jpg"; 

    System.Drawing.Image a = System.Drawing.Image.FromFile(rt); 
    Bitmap bitmapImage = new Bitmap(a); 

    System.Drawing.Image a1 = System.Drawing.Image.FromFile(rt1); 
    Bitmap bitmapImage1 = new Bitmap(a1); 



    List<string> list = new List<string>(); 


    for (int y = 0; y < bitmapImage.Height; y++) 
    { 
     for (int x = 0; x < bitmapImage.Width; x++) 
     { 
      pixelColor = bitmapImage.GetPixel(x, y); 
      pixelColor1 = bitmapImage1.GetPixel(x, y); 

이 오류가 발생합니다.

Line 168: webClient.DownloadFile(remote, local);" [IOException: The process cannot access the file

+0

정말 여기에 질문을 따르지 마십시오. 이 코드에서 사용중인 파일이 있지만 재정의로 수정할 수 있습니까? 우리에게 무엇이 필요합니까? – paqogomez

+0

이것은 "Line 168 : webClient.DownloadFile (remote, local);"오류입니다. [IOException : 프로세스가 파일 – user3130269

+0

에 액세스 할 수 없습니다. 어떤 행이 168 행입니까? – paqogomez

답변

2

문제는 webclient이 파일에 계속 매달려 있다는 것입니다.

웹 클라이언트를 폐기하여 리소스를 해제하십시오.

WebClient webClient = new WebClient(); 
string remote = "sample.jpg"; 
string px = Request.PhysicalApplicationPath.ToString(); 
if (File.Exists(px+"1.jpg") != true) 
{ 
    string local = px + "1.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
else 
{ 
    string local = px + "2.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
webClient.Dispose() 
+0

예, 실제로 처분에 숨어있는 문제 -하지만 다시는 웹 클라이언트가 아니지만 그래픽. 기본적으로이 문제가 해결됩니다. bitmapImage.Dispose(); bitmapImage1.Dispose(); a.Dispose(); a1.Dispose(); – user3130269

+1

그래서 bitmapimage와 "a"를 삭제하면 나중에 사용할 수 있도록 "2.jpg"가 해제됩니다. 나는이 문제가 실제로 브라우저가 "2.jpg"의 첫 번째 발생을 캐시하고 실제로 반복 된 변경된 그림이 감지되지 않기 때문에 객체를 파괴한다는 사실을 잘못 알았습니다. 그래서 브라우저 이미지 캐싱 문제를 해결해야했습니다. 나는 모든 것이 작동하지만 알고리즘의 모든 것이 정확한지보기 위해 3-4 번의 테스트가 필요하다는 것을 99 % 확신합니다. 30 분 정도 후에 다시 쓰고 답을 가장 잘 표시 할 것입니다. – user3130269

1

꽤 오래되었지만 대신 블록을 사용해보십시오.

using (WebClient client = new WebClient()) 
{ 
    client.DownloadFile(remotePath, localPath); 
} 
관련 문제