1 图片直接编译到silverlight应用程序的dll当中
Build Action: Resource (注意不要选择Embeded Resource,Silverlight无法识别该格式。)
于是在Xaml中可以这样引用图片
<Image Source="Images/grandpiano.jpg"></Image>
用C#引用:
img.source = new BitmapImage(new Uri("Images/grandpiano.jpg",UriKind.Relative));
或者:
img.source = new BitmapImage(new Uri("/SilverlightApplication1;component/Images/grandpiano.jpg",UriKind.Relative));
StreamResourceInfo sr = Application.GetResourceStream(new Uri("/SilverlightApplication1;component/Images/grandpiano.jpg",UriKind.Relative)); BitmapImage bmp = new BitmapImage(); bmp.SetSource(sr.Stream); img.source = bmp;
2 把图片资源放到silverlight应用程序的主包(Xap)中
在Xaml中可以这样引用图片
Image Source="/Images/grandpiano.jpg"></Image>
注意跟前面的区别:最前面的“/”代表xap文件的根路径。
用C#引用:
img.source = new BitmapImage(new Uri("Images/grandpiano.jpg"));
或者:
StreamResourceInfo sr = Application.GetResourceStream(new Uri("Images/grandpiano.jpg",UriKind.Relative)); BitmapImage bmp = new BitmapImage(); bmp.SetSource(sr.Stream); img.source = bmp;
注意在C#引用中没有最前面的“/”。
3 把图片放到web服务器上
/ClientBin/
App.Xap
Images/
build action: None
在Xaml中可以这样引用图片
<Image Source="/Images/grandpiano.jpg"></Image>
用C#引用:
img.source = new BitmapImage(new Uri("Images/grandpiano.jpg"));
方法3虽然与方法2代码相同,本质上已经根本不同。方法3中图片文件已经不在xap文件中,而是在web服务器上,意味着xap文件的尺寸已经大大减小,silverlight应用程序的加载时间大大缩小。缺点是这些图片因为会在引用时动态从服务器下载,直到下载完成才可使用,所以会导致延迟。不过由于Silverlight缓存资源的缘故,只有第一次引用会有延迟,再次引用时将直接从缓存中读取文件,没有丝毫延迟。
string uri = Application.Current.Host.source.AbsoluteUri; int index = uri.IndexOf("/ClientBin"); uri = uri.Substring(0,index) + "/Images/grandpiano.jpg"; // Begin the download. WebClient webClient = new WebClient(); webClient.OpenReadCompleted += webClient_OpenReadCompleted; webClient.OpenReadAsync(new Uri(uri));
private void webClient_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e) { if (e.Error != null) { // (Add code to display error or degrade gracefully.) } else { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(e.Result); img.source = bitmapImage; } }
webClient与浏览器共享缓存,即webClient会把下载的文件保存到浏览器缓存目录中,如果文件已经被缓存,再次调用OpenReadAsync方法时将直接中缓存中获得文件,这将大大减小获取文件的时间。但是从浏览器缓存获取文件仍然会有一点点延迟,通常不会被察觉,但是如果用于显示动画,这个延迟会导致严重闪烁。解决这个问题的办法就是把下载的资源保存到silverlight的内存中,引用时直接从内存读取。Application.Resources是个不错的选择,可以用于资源缓存。
4. 把图片打包放到web服务器上
void DownloadImagePart(string imgPart) { WebClient wc = new WebClient(); wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync(new Uri("imgs.zip",UriKind.Relative),imgPart); } void wc_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e) { StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream,null); String sURI = e.UserState as String; StreamResourceInfo imagestream = Application.GetResourceStream(sri,new Uri(sURI,UriKind.Relative)); BitmapSource imgsrc = new BitmapSource(); imgsrc.SetSource(imagestream.Stream); ImgToFill.source = imgsrc; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。