在我的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个问题:
- 有没有办法给e.Result直接复制到图片控件,或者我应该缓存图像,并使用缓存为源,或者我应该将图像保存到独立存储,然后使用为源,然后删除当我用它做的形象呢? 无论情况下,请你告诉我怎么用代码?
- 为GIF的和JPG的处理有所不同? 如果是这样,你能告诉我2种不同的方式?
如果你只是想显示的图片,你不必使用Web客户端。 您可以将图像源中直接设置URI和控制会照顾下载的:
imgLogo.Source = new BitmapImage(new Uri("images/yourPicture.png", UriKind.Relative));
需要注意的是GIF不受Image控件的支持。 :您仍然可以通过使用从ImageTools库中的转换器显示它们在使用Silverlight WP7一个应用程序显示GIF
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);
}
如果你想在这样的独立存储的使用,以节省
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;
比方说,你的图像控制被称为MyImage
,你可以做到这一点从一个URL加载图像:
MyImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://mySite/photo.jpg"));
不需要做所有的管道只是下载图像,框架已经这样做是为您服务!