2009-09-10 6 views
0

사용자가 이미지를 업로드 할 수있게 해주는 Silverlight 3 앱이 있습니다. 이미지는 (필자는 브라우저에서 볼 수 있습니다) 잘 업로드됩니다,하지만 난 실버 라이트에서 다음과 같은 오류가 발생합니다 :이미지를 Silverlight에 표시 할 수 없습니다.

메시지 : Sys.InvalidOperationException : IMAGEERROR 오류 # 4001 컨트롤 'Xaml1'에서 : AG_E_NETWORK_ERROR 라인 : 453 를 문자 : 17 코드 : 0 URI : http://www.greektools.net/ScriptResource.axd?d=J4B4crBmh4Qxr3eVyHw3QRgAKpCaa6XLu8H_2zpAs7eWeADXG9jQu_NCTxorhOs1x6sWoSKHEqAL37LcpWepfg2&t=fffffffffd030d68

피들러 보고서 :이 당신이 찾고있는 리소스에 문제가이며, 표시 할 수 없습니다.

미리 도움을 주셔서 감사합니다.

답변

1

여기 세 파일에 있습니다. 내 프로필에 나와있는 블로그에서도 사용할 수 있습니다.

XAML (imageuploader.xaml) :

<UserControl x:Class="ImageEditor.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" MouseMove="UserControl_MouseMove"> 

    <Grid x:Name="LayoutRoot" Background="Azure" Width="800" Height="600"> 
    <Image x:Name="MainImage" Source="images/image.jpg" Width="300" HorizontalAlignment="Center" VerticalAlignment="Center"></Image> 
    <Button x:Name="UploadImage" Click="UploadImage_Click" Content="Upload"></Button> 
    </Grid> 
</UserControl> 

코드 (imageuploader.xaml.cs) :

private void UploadImage_Click(object sender, RoutedEventArgs e) 
     { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      dlg.Multiselect = false; 
      dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png"; 

      bool? retval = dlg.ShowDialog(); 

      if (retval != null &amp;&amp; retval == true) 
      { 
       UploadFile(dlg.File.Name, dlg.File.OpenRead()); 
       StatusText.Text = dlg.File.Name; 
      } 
      else 
      { 
       StatusText.Text = "No file selected..."; 
      } 
     } 

     private void UploadFile(string fileName, Stream data) 
     { 
      string host = Application.Current.Host.Source.AbsoluteUri; 
      host = host.Remove(host.IndexOf("/ClientBin")); 

      UriBuilder ub = new UriBuilder(host + "/receiver.ashx"); 
      ub.Query = string.Format("filename={0}", fileName); 
      WebClient c = new WebClient(); 
      c.OpenWriteCompleted += (sender, e) =&gt; 
      { 
       PushData(data, e.Result); 
       e.Result.Close(); 
       data.Close(); 
      }; 
      c.WriteStreamClosed += (sender, e) =&gt; 
      { 
       LoadImage(fileName); 
      }; 
      c.OpenWriteAsync(ub.Uri); 
     } 

     private void LoadImage(string fileName) 
     { 
      // 
      // Creating WebClient object and setting up events 
      // 
      WebClient downloader = new WebClient(); 
      downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted); 
      //downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged); 

      // 
      // Specify Image to load 
      // 
      string host = Application.Current.Host.Source.AbsoluteUri; 
      host = host.Remove(host.IndexOf("/ClientBin")); 

      fileName = host + "/images/" + fileName; 
      downloader.OpenReadAsync(new Uri(fileName, UriKind.Absolute)); 
     } 

     void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
     { 
      // 
      // Create a new BitmapImage and load the stream 
      // 
      BitmapImage loadedImage = new BitmapImage(); 
      loadedImage.SetSource(e.Result); 

      // 
      // Setting our BitmapImage as the source of a imageControl control I have in XAML 
      // 
      MainImage.Source = loadedImage; 
     } 

     private void PushData(Stream input, Stream output) 
     { 
      byte[] buffer = new byte[4096]; 
      int bytesRead; 

      while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0) 
      { 
       output.Write(buffer, 0, bytesRead); 
      } 
     } 

처리기 (receiver.ashx) :

[WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    public class receiver : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      string filename = context.Request.QueryString["filename"].ToString(); 

      using (FileStream fs = File.Create(context.Server.MapPath("~/images/" + filename))) 
      { 
       SaveFile(context.Request.InputStream, fs); 
      } 
     } 

     private void SaveFile(Stream stream, FileStream fs) 
     { 
      byte[] buffer = new byte[4096]; 
      int bytesRead; 
      while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) 
      { 
       fs.Write(buffer, 0, bytesRead); 
      } 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 


    } 
관련 문제