C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十八) 完美精灵之八面玲珑(WPF Only)②
2010-10-01 07:07:47 来源:WEB开发网有了承接角色的载体,下面就是如何对上一节中合成的角色大图与武器大图(提取及合成方法同上一节相同)进行拼装,最后分帧存储进PartImage。嘿嘿,又现精华:
/// <summary>
/// 拼装角色+装备后切割成系列帧图片并保存进内存(装备角色)
/// </summary>
/// <param name="Equipment">装备代号数组</param>
/// <param name="rowNum">帧合成图行数</param>
/// <param name="colNum">帧合成图列数</param>
/// <param name="totalWidth">帧图合成后总宽</param>
/// <param name="totalHeight">帧图合成后总高</param>
/// <param name="singleWidth">单帧图宽</param>
/// <param name="singleHeight">单帧图高</param>
/// <returns>如果缓存中有则读取缓存,否则返回合成的图片源</returns>
public static BitmapSource[,] EquipPart(int[] Equipment, int rowNum, int colNum, int totalWidth, int totalHeight, int singleWidth, int singleHeight) {
//Equipment[0]为衣服代号,Equipment[1]为武器代号,本例中装备只由衣服+武器组成
//假如内存中没有该装备的角色现成图片源则进行读取
if (PartImage[Equipment[0], Equipment[1]] == null) {
BitmapSource[,] bitmap = new BitmapSource[rowNum, colNum];
//加载角色衣服(身体)大图
BitmapSource bitmapSource = new BitmapImage(new Uri(@"ImagesBody" + Equipment[0].ToString() + ".gif", UriKind.Relative));
//假如武器不是0,即如果角色手上有武器而非空手
if (Equipment[1] != 0) {
//加载武器大图,并与衣服大图组装
BitmapSource bitmapSource1 = new BitmapImage(new Uri(@"ImagesWeapon" + Equipment[1].ToString() + ".gif", UriKind.Relative));
DrawingVisual drawingVisual = new DrawingVisual();
Rect rect = new Rect(0, 0, totalWidth, totalHeight);
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(bitmapSource, rect);
drawingContext.DrawImage(bitmapSource1, rect);
drawingContext.Close();
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(totalWidth, totalHeight, 0, 0, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
bitmapSource = renderTargetBitmap;
//降低图片质量以提高系统性能(由于本身图片已经为低质量的gif类型,因此效果不大)
//RenderOptions.SetBitmapScalingMode(bitmapSource, BitmapScalingMode.LowQuality);
}
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
bitmap[i, j] = new CroppedBitmap(bitmapSource, new Int32Rect(j * singleWidth, i * singleHeight, singleWidth, singleHeight));
}
}
//将装备合成图放进内存
PartImage[Equipment[0], Equipment[1]] = bitmap;
return bitmap;
} else {
//如果内存中已存在该装备的角色图片源则从内存中返回合成图,极大提高性能
return PartImage[Equipment[0], Equipment[1]];
}
}
Tags:开发 WPF Silverlight
编辑录入:爽爽 [复制链接] [打 印]- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››WPF学习笔记12. Binding (5)
- ››WPF学习笔记11. Binding (4)
- ››WPF学习笔记10. Binding (3)
- ››WPF学习笔记9. Binding (2)
- ››WPF学习笔记8. Binding
更多精彩
赞助商链接