Web客户端将JPG和GIF加载到图像中
本文关键字:加载 图像 GIF 客户端 JPG Web | 更新日期: 2023-09-27 18:32:01
在我的 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 直接复制到 Image 控件,或者我应该缓存图像并使用缓存作为源,还是应该将图像保存到独立存储,然后将其用作源,然后在完成后删除图像?无论哪种情况,您能否告诉我如何使用代码?
- GIF 和 JPG 的处理方式不同吗?如果是这样,你能告诉我两种不同的方式吗?
如果您只想显示图片,则不必使用 WebClient。可以直接在镜像源中设置 Uri,控件将负责下载:
imgLogo.Source = new BitmapImage(new Uri("images/yourPicture.png", UriKind.Relative));
请注意,图像控件不支持 GIF。您仍然可以使用 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"));
无需为了下载图像而进行所有管道工作,框架已经为您完成了这项工作!