简体   繁体   English

在Silverlight中无法显示图像

[英]Image Cannot be Displayed in Silverlight

I have a Silverlight 3 app that allows the user to upload images, and then is supposed to display them. 我有一个Silverlight 3应用程序,该应用程序允许用户上传图像,然后显示这些图像。 The image gets uploaded fine (I can view it in the browser), but I get the following error from Silverlight: 图像可以很好地上传(我可以在浏览器中查看),但是从Silverlight得到以下错误:

Message: Sys.InvalidOperationException: ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR Line: 453 Char: 17 Code: 0 URI: http://www.greektools.net/ScriptResource.axd?d=J4B4crBmh4Qxr3eVyHw3QRgAKpCaa6XLu8H_2zpAs7eWeADXG9jQu_NCTxorhOs1x6sWoSKHEqAL37LcpWepfg2&t=fffffffffd030d68 消息: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

Fiddler reports: There is a problem with the resource you are looking for, and it cannot be displayed. Fiddler报告:您正在寻找的资源有问题,无法显示。

Thanks in advance for your help. 在此先感谢您的帮助。

Here it is in three files. 这是三个文件。 This is also available at my blog, which is listed in my profile. 这也可以在我的博客中找到,该博客列在我的个人资料中。

XAML (imageuploader.xaml): 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>

Code (imageuploader.xaml.cs): 代码(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);
            }
        }

Handler (receiver.ashx): 处理程序(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;
            }
        }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM