2013-01-19 4 views
0

C#의 경우 약간의 문제가 있습니다.배경이 업데이트되지 않음 C#

사용자가 지정한 이미지를 다운로드하여 내 양식의 배경을 동적으로 업데이트하려고합니다. 왜 내 양식이 업데이트 된 배경을 표시하지 않습니다에 관한

public bool getImgFromWeb(string url) 
    { 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url, UriKind.Absolute)); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      //if response is okay, and it's an image 
      //sometimes 404 will be okay, but will redirect to website. 
      if ((response.StatusCode == HttpStatusCode.OK) && 
       (response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))) 
      { 
       Bitmap tempImg = new Bitmap(response.GetResponseStream()); 
       this.BackgroundImage = tempImg; //this line does nothing. 
       this.Invalidate(); //to force the window to redraw 
      } 
      else 
      { 
       MessageBox.Show("Sorry, the image your are trying to download does not exist. Please re-enter the image URL."); 
       return false; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Sorry, an error: " + ex.Message + " occurred."); 
      return false; 

     } 

제안 : (양식 및 업데이트) 이미지를 다운로드하려면 코드

은 다음과 같습니다?

감사합니다.

+0

getImgFromWeb은 무엇을 호출합니까? 다른 메모에서는 GetImgFromWeb에 대한 메서드의 이름을 표준 C# 명명 규칙을 따르도록 변경해야합니다. –

+0

실제 예외는 무엇입니까? – daryal

+0

'this.Show();'를 코드에 추가하면 폼을 볼 수 있습니다. 잘못된 형식입니다. –

답변

1

시나리오를 복제하고 this.Refresh()를 사용하여 this.Invalidate()를 대체했으며 작동했습니다. Visual Studio 2012에 있습니다.

private void SetImageAsBackground(string uri) 
    { 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      if (response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase)) 
      { 
       Bitmap temp = new Bitmap(response.GetResponseStream()); 
       this.BackgroundImage = temp; 
       this.Refresh(); 
      } 
      else 
      { 
       MessageBox.Show("This isn't an image!"); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(string.Format("Exception: {0}", ex));     
     } 
    } 
+0

감사! 정말 잘 돌아갔습니다! – docaholic

관련 문제