传统的基于GDI或GDI+的图形开发大家再熟悉不过了,通过Drawing类可以轻松的对位图进行处理,那么在WPF中的Media类是否也拥有同样类似的方法功能呢?这是肯定的。
本节我以精灵装备合成Show(纸娃娃)为例向大家演示如何在WPF中实现基于像素的图象合成。 首先准备一下合成用的素材:
接下来是进行绘图的第一步:创建绘图工具实例并开启绘图:
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
接着按深度顺序从底到外逐个对象绘制:
BitmapSource weapon = GetImage(
string.Format("Image/Weapon_{0}.png",
(comboBox1.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(weapon, new Rect(18, 18, 120, 120));
通过以上代码,我们将位于"Image/Weapon_{0}.png"的武器图片绘制到一个x,y方向偏移量分别为(18,18)的120*120像素的矩形上,其中的GetImage方法为:
//图片加载
private BitmapSource GetImage(string uri) {
try {
return BitmapFrame.Create(new Uri(string.Format(@"{0}", uri), UriKind.Relative));
} catch {
return null;
}
}
然后通过相类似的代码再分别依次绘制身体、左手、右手、头部、坐骑等部位:
//绘制身体
BitmapSource body = GetImage(
string.Format("Image/Body_{0}.png",
(comboBox2.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(body, new Rect(50, 50, 100, 100));
//绘制左手
BitmapSource LeftHand = GetImage(
string.Format("Image/LeftHand_{0}.png",
(comboBox3.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(LeftHand, new Rect(72, 52, 30, 30));
//绘制右手
BitmapSource RightHand = GetImage(
string.Format("Image/RightHand_{0}.png",
(comboBox4.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(RightHand, new Rect(98, 59, 30, 30));
//绘制头部
BitmapSource Head = GetImage(
string.Format("Image/Head_{0}.png",
(comboBox5.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(Head, new Rect(86, 38, 20, 30));
//绘制坐骑
BitmapSource Horse = GetImage(
string.Format("Image/Horse_{0}.png",
(comboBox6.SelectedItem as ComboBoxItem).Content)
);
drawingContext.DrawImage(Horse, new Rect(63, 71, 70, 120));
所有对象绘制完后即可以关闭画板并呈现出来:
drawingContext.Close();
RenderTargetBitmap composeImage = new RenderTargetBitmap(200, 200, 0, 0, PixelFormats.Pbgra32);
composeImage.Render(drawingVisual);
最后将composeImage这个合成的图片作为图片源赋值给精灵即完成了整个合成流程:
Spirit.Source = composeImage;
实现后的系统界面如下:
本节以展示如何在WPF中对像素位图进行合成,由于素材资源有限,因此无法为大家演示动画效果的纸娃娃图片合成系统。但是万变不离其中,只要您掌握了本文的方法并合理的处理好图片组的缓存,这将为您的图片合成带来更高的性能与效果。
下一节我将为大家讲解如何在Silverlight中实现一模一样的效果,这就是传说中的移植,敬请关注.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/alamiye010/archive/2009/09/20/4572834.aspx
分享到:
相关推荐
arcgis runtime for wpf
ComponentOne Studio for WPF2012 v3 一共有 3 个压缩文件,请全部下载后解压 ComponentOne Studio WPF2012 v3 Leveraging the full potential of Windows Presentation Foundation (WPF), ComponentOne Studio®...
wpf图像四顶点透视变换,wpf图像四顶点透视变换,wpf图像四顶点透视变换
ComponentOne Studio for WPF2012 v3 一共有 3 个压缩文件,请全部下载后解压 ComponentOne Studio WPF2012 v3 Leveraging the full potential of Windows Presentation Foundation (WPF), ComponentOne Studio®...
ComponentOne Studio for WPF 注册码
DotNetBar 5.3.0.2 for WPF.这个是WPF版本的。
WPF基于Vlc.DotNet.Wpf封装的视频播放器
Infragistics NetAdvantage for WPF Volume 1第二部分
Xceed DataGrid for WPF v3.5
arcgis runtime sdk for wpf 10.2.5 安装文件,esri官网已放弃,目前官网无法下载
Modern UI for WPF一个国外的开源界面,界面简洁、清新,嵌入自己的项目十分简单,可很好的嵌入多个不同页面,将多个软件集合成一个,用起来非常棒,非常的喜欢。
解压文件中会包含Prism5ClassLibrary.chm帮助文档和很多优秀示例程序
这里是官网下载的资源包,三个文件,一个为文档。... WindowChrome类,可以将 WPF 通过扩展客户端区域以覆盖非客户端区域窗口框架中的内容。 同时,它将保留两个不可见区域; 通过系统行为调整大小边框并标题区域。
Telerik UI for WPF 2018_1_116 .
RadControls for WPF Q2 2013.2.0611 开发版
基于.net core 3.1 的WPF UI控件库
Telerik RadControls for WPF 4.0 2011 Q1 V2011.1.0419 源代码 2/3,下载完该附件,请先解压为Telerik RadControls for WPF 4.0 2011 Q1 V2011.1.0419 源代码.7z.002。 很奇怪,这个文件上传了许多次,都提示此文件...
Xceed DataGrid for WPF v3.5 Part.1
一个基于Google Chrome内核的WPF浏览器开发,包括源码就完整的插件文件,可以直接使用。