2012-04-28 4 views
6

내 Wp7 앱에서 웹 브라우저 컨트롤을 사용하고 있지만 웹 브라우저의 이미지 디렉토리에있는 이미지를 넣을 수 없습니다.웹 브라우저 컨트롤에서 로컬 이미지 사용

일부 이미지는 .cs 및 .xaml 파일과 같은 디렉토리에있는 폴더에 저장했습니다. 이제는 웹 브라우저 컨트롤에 넣으려고하지만이 기능을 사용할 수없는 것 같습니다. 다른 무언가로 대체해야

<img src="/SilverlightApplication;component/photo.jpg"/> 

"SilverlightApplication"및 "구성 요소"하지만 I :

<img src="images/photo.jpg"/> 
<img src="/images/photo.jpg"/> 

분명 위의 두 가지 작동하지 않는, 내 생각은 이런 식으로 뭔가해야한다는 것입니다 .

- :

답변

7
당신은 격리 된 저장소에 이미지를 저장하고 거기에서 이미지를 표시해야합니다

내가 함께 다음 위치에서 다운로드 할 수있는 샘플을 넣어 가지고

을 :(모르겠어요

www.smartmobiledevice.co.uk/projects/webbrowserimagesample.zip

이 내용은 MSDN 문서 How to: Display Static Web Content Using the WebBrowser Control for Windows Phone을 기반으로합니다.

+0

대단히 감사합니다. Paul! 하지만 WebBrowser에서 NavigateToString 메서드를 수행하고 문자열에 전체 HTML 페이지가 있습니다. 어떤 이유로 이미지가 그런 식으로 작동하지 않습니다. : S – MrCornholio

+0

btw. 격리 된 스토리의 이미지 만 NavigateToString에서 작동하지 않습니다. 절대적인 우 리가있는 일들은 일을합니다. – MrCornholio

+0

문자열을 Isolated Storage의 파일에도 저장하고 WebBrowser 컨트롤의 Base 속성을 파일 (HTML, 이미지, CSS, 자바 스크립트)을 저장 한 로컬 디렉터리로 설정해야합니다. – MatthieuGD

0

아니라 동적 배열을 업데이트 위에서 주어진 폴 예제 응용 프로그램을 연결하여 역동적 인 이미지를 사용 할 수

string[] files = { 
     "readme.htm", "desert.jpg", "sample.jpg" 
    }; 

하고 삭제할 수 있습니다 격리에 기록하기 전에

기존

 private void SaveFilesToIsoStore() 
    { 
     //These files must match what is included in the application package, 
     //or BinaryStream.Dispose below will throw an exception. 
     string[] files = { 
     "readme.htm", "desert.jpg", "sample.jpg" 
    }; 

     IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
     if(isoStore.FileExists(files[0])) 
     { 
      isoStore.DeleteFile(files[0]); 
     } 

      foreach (string f in files) 
      { 
       StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative)); 
       using (BinaryReader br = new BinaryReader(sr.Stream)) 
       { 
        byte[] data = br.ReadBytes((int)sr.Stream.Length); 
        SaveToIsoStore(f, data); 
       } 
      } 
     } 


    private void SaveToIsoStore(string fileName, byte[] data) 
    { 
     string strBaseDir = string.Empty; 
     string delimStr = "/"; 
     char[] delimiter = delimStr.ToCharArray(); 
     string[] dirsPath = fileName.Split(delimiter); 

     //Get the IsoStore. 
     IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 

     //Re-create the directory structure. 
     for (int i = 0; i < dirsPath.Length - 1; i++) 
     { 
      strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]); 
      isoStore.CreateDirectory(strBaseDir); 
     } 

     //Remove the existing file. 
     if (isoStore.FileExists(fileName)) 
     { 
      isoStore.DeleteFile(fileName); 
     } 

     //Write the file. 
     using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName))) 
     { 
      bw.Write(data); 
      bw.Close(); 
     } 
    } 
5

에 일부 WinRT 클래스를 사용할 수있는 Windows Phone 8에서는 앱의 격리 된 저장소에 대한 파일 시스템 경로를 얻을 수 있습니다. 그래서 IsoStorage에있는 파일에 대한 절대 URL은 다음과 같습니다

string URL = "file://" + 
    Windows.Storage.ApplicationData.Current.LocalFolder.Path + 
    "\\folder\\filename.png"; 

WebBrowser 컨트롤은 NavigateToString() 이러한 URL을한다 'HTML 괜찮을 거라고. 또는 IsoStorage를 기본으로 지정하고 전체 URL을 사용할 수 있습니다. isostore: URL이 작동하지 않습니다. 시도했습니다. ms-appx://local/도 마찬가지입니다.

완벽하게 말해서 앱의 리소스에 대한 파일 시스템 경로를 매우 유사하게 얻을 수 있습니다. 그게 Windows.ApplicationModel.Package.Current.InstalledLocation.Path입니다.

+0

좋은 답변입니다. 'NavigateToString'과 완벽하게 작동합니다. 대단히 감사합니다 :-) –

+0

@SevaAlekseyev 중요 : 위의 기술은 로컬 폴더와 그 아래의 로컬 폴더에있는 형제가 아닌 저장소의 다른 디렉토리에서는 작동하지 않습니다. 예를 들어 Path.GetTempPath() 메서드를 사용하여 Temp라는 Local에 형제 디렉터리를 만들었습니다. 해당 디렉토리의 파일은 WebBrowser 구성 요소에서 액세스 할 수 없었습니다. 로컬 폴더에 복사하면 문제가 해결됩니다. –

0

이것은 매우 오래된 스레드이지만, 방금 WP7 앱을 업데이트 한 것처럼 해결책을 찾았습니다. 이제

private string GetBase64(string f) 
    { 
     string ret = ""; 

     { 
      StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative)); 
      using (BinaryReader br = new BinaryReader(sr.Stream)) 
      { 
       byte[] data = br.ReadBytes((int)sr.Stream.Length); 
       ret = System.Convert.ToBase64String(data); 
      } 
     } 
     return ret; 
    } 

, 당신은 코드에 이미지를 삽입 할 (내있는 GIF를)이 사용

비밀은 64 표현 먼저베이스에 이미지를 변환, 그래서이 코드로 시작하는 것입니다

StringBuilder sb = ... // a string builder holding your webpage 
String b64 = GetBase64("assets/images/filename.gif"); 

sb.AppendFormat(@"<img src='data:image/gif;base64,{0}' /></div>", b64); 
관련 문제