没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|2010-02-03 10:43:16.000|阅读 1111 次
概述:希望此文对投身于Silverlight游戏的朋友予以帮助。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
希望此文对投身于Silverlight游戏的朋友予以帮助。
代码下载:SilverlightTestWbResource.zip
目录设置如下:
情形1:将png文件都设置为Resource,Do not copy。
指定Image的Source代码:
spirit.Source = new BitmapImage(new Uri(@"Player/" + count + ".png", UriKind.Relative));
注意这里使用的是相对路径,是当前MainPage.xaml相对于图片的位置,而不能使用服务器路径。
如果我们在Spirit中指定Image的Source,就是说相对路径变了,那么代码要写为:
Body.Source = new BitmapImage(new Uri(@"../Player/2.png", UriKind.Relative));
在这种情形下,所有图片都被作为资源而嵌入到dll中,观察xap中的组织结构,可以证实我们的结论:
情形2:将png文件都设置为Content,Copy if newer。
指定Image的Source代码:
spirit.Source = new BitmapImage(new Uri(@"/Player/" + count + ".png", UriKind.Relative));
注意这里使用的是服务器路径,而不能使用相对路径。
使用服务器路径的一个好处是,无论xaml位于那个目录下,都可以无视,例如上面的那个Spirit控件:
Body.Source = new BitmapImage(new Uri(@"/Player/2.png", UriKind.Relative));
在这种情形下,所有图片都打包到xap的Player目录下,观察xap中的组织结构,可以证实我们的结论:
其中,Player文件夹中存放着0-7这8张png图片
下面分析截图的技术 ,就是说把一张大图,只截取其中一部分。
有2种方法:
方法1:使用Clip 技术。
public partial class MainPage : UserControl { private int count = 1; private Image spirit; public MainPage() { InitializeComponent(); spirit = new Image() { Width = 1500, Height = 150, Source = new BitmapImage(new Uri(@"Player/PlayerMagic.png", UriKind.Relative)) }; Carrier.Children.Add(spirit); DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += dispatcherTimer_Tick; dispatcherTimer.Interval = TimeSpan.FromMilliseconds(150); dispatcherTimer.Start(); } void dispatcherTimer_Tick(object sender, EventArgs e) { spirit.Clip = new RectangleGeometry() { Rect = new Rect(count * 150, 0, 150, 150) }; spirit.RenderTransform = new TranslateTransform() { X = -count * 150 }; count = count == 7 ? 0 : count + 1; } }
其中,使用相对路径或服务器路径都是可以的,当然,相应地,图片要设置为Resource或Content。
方法2:使用WriteableBitmap技术
这个WriteableBitmap比较玄的,在使用中有很多地方需要注意。我和深蓝色右手昨天差点被它搞死,拿到QQ群讨论,才知道很多人都有同样的困扰和迷惑。
1)使用服务器路径的编程模型:
public partial class MainPage : UserControl { private int count = 1; private Image spirit; public MainPage() { InitializeComponent(); spirit = new Image() { Width = 150, Height = 150 }; Carrier.Children.Add(spirit); DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += dispatcherTimer_Tick; dispatcherTimer.Interval = TimeSpan.FromMilliseconds(150); dispatcherTimer.Start(); } void dispatcherTimer_Tick(object sender, EventArgs e) { //spirit.Source = new BitmapImage(new Uri(@"/Images/" + count + ".png", UriKind.Relative)); spirit.Source = ComposeEquip(@"/Images/Body0.png", 150, 150, new Point(-count * 150, 0)); count = count == 33 ? 0 : count + 1; } public static WriteableBitmap ComposeEquip(string bodyAddress, int width, int height, Point bodyOffset) { WriteableBitmap writeableBitmap = new WriteableBitmap(width, height); writeableBitmap.Render(new Image() { Source = GetImage(bodyAddress) }, new TranslateTransform() { X = bodyOffset.X, Y = bodyOffset.Y }); writeableBitmap.Invalidate(); return writeableBitmap; } public static BitmapSource GetImage(string address) { return new BitmapImage(new Uri(string.Format(@"{0}", address), UriKind.Relative)); } }
如果我们还想合并2张图片,那么ComposeEquip方法可能就是下面这样的:
public static WriteableBitmap ComposeEquip(string bodyAddress, string weaponAddress, int width, int height, Point bodyOffset, Point weaponOffset) { WriteableBitmap writeableBitmap = new WriteableBitmap(width, height); writeableBitmap.Render(new Image() { Source = Super.GetImage(bodyAddress) }, new TranslateTransform() { X = bodyOffset.X, Y = bodyOffset.Y }); writeableBitmap.Render(new Image() { Source = Super.GetImage(weaponAddress) }, new TranslateTransform() { X = weaponOffset.X, Y = weaponOffset.Y }); writeableBitmap.Invalidate(); return writeableBitmap; }
2)使用相对路径的编程模型:
如何把上面的路径换成相对路径,同时把图片修改为Resource,就玩不转了。
我猜测,问题出在writeableBitmap.Render(….);和writeableBitmap.Invalidate();这两条语句之间。因为Render需要时间,
那么如何解决呢?
有人出馊主意,说在这2条语句之间开一个线程,然后sleep1秒,但是他没有考虑到我们的ComposeEquip就是一个每150mm就执行一次的方法。
正确的解决方案如下:
我们为要加载的大图片的ImageOpened 事件上挂上一个方法,在该方法中,我们把这张大图片切割成若干小图片,并放到frames 数组中——这时大图片是可以使用的,所以不用担心切割的时候是一片空白。
还要注意,bitmap_ImageOpened方法什么时候触发呢?我们看到MainPage构造函数的最后一条语句:
spirit.Source = bitmap;
对,就是执行这条语句的时候触发——不信?那你就把这句话注释掉,看看bitmap_ImageOpened方法还能被执行到么。我是没看到断点会停到这里。
public partial class MainPage : UserControl { private int count = 1; private Image spirit; ImageSource[] frames = new ImageSource[10]; public MainPage() { InitializeComponent(); spirit = new Image(); BitmapImage bitmap = new BitmapImage(new Uri(@"Src/PlayerMagic.png", UriKind.Relative)); bitmap.ImageOpened += new EventHandler<RoutedEventArgs>(bitmap_ImageOpened); Carrier.Children.Add(spirit); spirit.Visibility = Visibility.Collapsed; spirit.Source = bitmap; } void dispatcherTimer_Tick(object sender, EventArgs e) { spirit.Source = frames[count]; count = count == 7 ? 0 : count + 1; } void bitmap_ImageOpened(object sender, RoutedEventArgs e) { spirit.Source = sender as BitmapImage; for (int j = 0; j < 10; j++) { WriteableBitmap wb = new WriteableBitmap(150, 150); wb.Render(spirit, new TranslateTransform() { X = -150 * j }); wb.Invalidate(); frames[j] = (ImageSource)wb; } DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = TimeSpan.FromMilliseconds(150); //重复间隔 dispatcherTimer.Start(); spirit.Source = frames[count]; spirit.Visibility = Visibility.Visible; } }
话说,我是在学习深蓝色右手的游戏教程时,把他的WPF版本转化为Silverlight版,在这期间,发现了这个问题。深蓝色右手昨晚交给我一个任务,就是把这个研究明白,然后给出一个终极答案。于是便有了这篇文章。
回顾一下本文的要点,
1.关于图片,Resource和Content的区别
2.Clip和WriteableBitmap的适用场合
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:互联网面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号