WEB开发网
开发学院软件开发C语言 C#开发WPF/Silverlight动画及游戏系列教程(Game C... 阅读

C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十八) 完美精灵之八面玲珑(WPF Only)②

 2010-10-01 07:07:47 来源:WEB开发网   
核心提示: 有了承接角色的载体,下面就是如何对上一节中合成的角色大图与武器大图(提取及合成方法同上一节相同)进行拼装,C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十八) 完美精灵之八面玲珑(WPF Only)②(2),最后分帧存储进PartImage,嘿嘿,即如果

有了承接角色的载体,下面就是如何对上一节中合成的角色大图与武器大图(提取及合成方法同上一节相同)进行拼装,最后分帧存储进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]];
            }
        }

上一页  1 2 3 4 5 6 7  下一页

Tags:开发 WPF Silverlight

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接