0

在我的 XAML 中,我有:

<Image Height="150" HorizontalAlignment="Left" Margin="0,4,0,0" Name="imgLogo" Stretch="Fill" VerticalAlignment="Top" Width="417" />
<Image Height="343" HorizontalAlignment="Left" Margin="0,155,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="417" />

在后面的 C# 代码中,我有:

WebClient wcForLogo = new WebClient();
wcForLogo.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForLogo_DownloadStringCompleted);
wcForLogo.DownloadStringAsync(new Uri("http://mySite/logo.gif"));

WebClient wcForPhoto = new WebClient();
wcForPhoto.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForPhoto_DownloadStringCompleted);
wcForPhoto.DownloadStringAsync(new Uri("http://mySite/photo.jpg"));

但现在我不知道如何捕捉图像并将其发布到我构建的 XAML 控件中。

2个问题:

  1. 有没有办法将 e.Result 直接复制到 Image 控件,或者我应该缓存图像并将缓存用作源,还是应该将图像保存到隔离存储然后将其用作源,然后删除完成后的图像?无论哪种情况,你能告诉我如何使用代码吗?
  2. GIF 和 JPG 的处理方式不同吗?如果是这样,你能告诉我两种不同的方式吗?
4

4 回答 4

2

如果只想显示图片,则不必使用 WebClient。您可以直接在图像源中设置 Uri,控件将负责下载:

imgLogo.Source = new BitmapImage(new Uri("images/yourPicture.png", UriKind.Relative));

请注意,图像控件不支持 GIF。您仍然可以使用 ImageTools 库中的转换器来显示它们:使用Silverlight 在 WP7 应用程序中显示 GIF

于 2012-04-24T09:09:55.987 回答
1
using System.Net;
using System.IO;
        private void Form1_Load(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.DownloadDataAsync(new Uri("http://mySite/logo.gif"));
            webclient.DownloadDataCompleted += callback;

        }
        void callback(object sender,DownloadDataCompletedEventArgs e)
        {
            var ms = new MemoryStream(e.Result);
            pictureBox1.Image = Image.FromStream(ms);
        }
于 2012-04-24T09:01:23.033 回答
0

如果您想保存在隔离存储中,请像这样使用

            WebClient m_webClient = new WebClient();

            Uri m_uri = new Uri("http://URL");

            m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

            m_webClient.OpenReadAsync(m_uri);




        }



    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        int count;

        Stream stream = e.Result;

        byte[] buffer = new byte[1024];


        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {




            using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("IMAGES.jpg", FileMode.Create, isf))
            {
                count = 0;

                while (0 < (count = stream.Read(buffer, 0, buffer.Length)))
                {
                    isfs.Write(buffer, 0, count);
                }

                stream.Close();
                isfs.Close();
            }
        }

要获取图像形式 isostore:

字节[] 数据;

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {

            using (IsolatedStorageFileStream isfs = isf.OpenFile(uri, FileMode.Open, FileAccess.Read))
            {
                data = new byte[isfs.Length];
                isfs.Read(data, 0, data.Length);
                isfs.Close();
            }

        }


        MemoryStream ms = new MemoryStream(data);

        BitmapImage bi = new BitmapImage();

        bi.SetSource(ms);

If you give the image name as image then set the source as bi:

        image.source = bi;

如果要直接添加

  WebClient client = new WebClient();
  Stream stream = client.OpenRead(imageUrl);
  Bitmap bitmap = new Bitmap(stream);
  image.source = bitmap;
于 2012-04-24T08:59:48.610 回答
0

假设您的图像控件被调用MyImage,您可以这样做以从 URL 加载图像:

MyImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://mySite/photo.jpg"));

无需为下载图像而做所有的工作,框架已经为您完成了!

于 2012-04-24T09:08:21.557 回答