private void drawAnimate(object sender, EventArgs e) { if (page != 3) { graphics.Clear(Color.Transparent); timer.Stop(); return; } int standardX = canvas.Width / 2, standardY = canvas.Height / 2; // 清空graphics graphics.Clear(Color.Transparent); // 绘制enemy graphics.DrawImage(Resources.enemy, standardX - 16, standardY - 16); // 进行绘制 Animation animation = animations[id]; if (index >= animation.frame_max) { index = 0; } Animation.Frame frame = animation.frames[index]; ColorMatrix matrix = new ColorMatrix(); ImageAttributes attributes = new ImageAttributes(); foreach (var frameInfo in frame.cell_data) { if (frameInfo.type < 0 || frameInfo.type >= bitmaps.Count) { continue; } int centerX = standardX + frameInfo.xpos, centerY = standardY + frameInfo.ypos; if (animation.position == 0) { centerY -= 16; } if (animation.position == 2) { centerY += 16; } int width = 192 * frameInfo.zoom / 100; matrix.Matrix33 = frameInfo.opacity / 255f; attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); var image = frameInfo.mirror ? bitmap_revs[frameInfo.type] : bitmaps[frameInfo.type]; var angle = frameInfo.angle; if (angle > 0) { graphics.TranslateTransform(centerX, centerY); graphics.RotateTransform(360 - angle); graphics.TranslateTransform(-centerX, -centerY); } graphics.DrawImage(image, new Rectangle(centerX - width / 2, centerY - width / 2, width, width), 0, 0, 192, 192, GraphicsUnit.Pixel, attributes); graphics.ResetTransform(); } canvas.Image = canvasMap; index++; }
private bool checkValidAnimate() { Animation animation = animations[id]; // File exists if ("".Equals(animation.name) || "".Equals(animation.animation_name)) { return(false); } string pngPath = Path.Combine(directory, animation.animation_name + ".png"); if (!File.Exists(pngPath)) { MessageBox.Show(pngPath + "不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } // Check position if (animation.position == 3) { MessageBox.Show("“位置:画面”不被支持!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } rgssReader.selectId(id); if (animation.frames == null) { MessageBox.Show("无效的动画", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } // 检查是否存在“旋转角度”、“左右翻转”、“合成方式” /* * string reason = null; * foreach (var animationFrame in animation.frames) * { * foreach (var frameInfo in animationFrame.cell_data) * { * if (reason != null) break; * // if (frameInfo.angle != 0) reason = "不支持带旋转角度的帧!"; * // if (frameInfo.mirror) reason = "不支持带翻转的帧"; * // if (frameInfo.blend != 1) reason = "只支持合成方式:加法!"; * } * } * if (reason != null) * { * MessageBox.Show(reason, "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); * return false; * } * */ // bitmap = new Bitmap(pngPath); Bitmap _bitmap = (Bitmap)Image.FromFile(pngPath); if (_bitmap.Width % 192 != 0 || _bitmap.Height % 192 != 0) { MessageBox.Show("目标图片的宽或高不是192的倍数!", "加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); _bitmap.Dispose(); return(false); } if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(_bitmap.Width, _bitmap.Height); Graphics graphics = Graphics.FromImage(bitmap); graphics.DrawImage(_bitmap, new Rectangle(0, 0, _bitmap.Width, _bitmap.Height), 0, 0, _bitmap.Width, _bitmap.Height, GraphicsUnit.Pixel); graphics.Dispose(); _bitmap.Dispose(); // 清空原有的 if (bitmaps == null) { bitmaps = new List <Bitmap>(); } else { bitmaps.ForEach(x => x.Dispose()); bitmaps.Clear(); } if (bitmap_revs == null) { bitmap_revs = new List <Bitmap>(); } else { bitmap_revs.ForEach(x => x.Dispose()); bitmap_revs.Clear(); } int hue = animation.animation_hue; BitmapWrapper bitmapWrapper = new BitmapWrapper(bitmap); for (int y = 0; y < bitmap.Height / 192; y++) { for (int x = 0; x < bitmap.Width / 192; x++) { Bitmap map = new Bitmap(192, 192); BitmapWrapper mapWrapper = new BitmapWrapper(map); Bitmap map_rev = new Bitmap(192, 192); BitmapWrapper mapWrapper_rev = new BitmapWrapper(map_rev); for (int u = 0; u < 192; u++) { for (int v = 0; v < 192; v++) { Color color = Util.addHue(bitmapWrapper.GetPixel(192 * x + u, 192 * y + v), hue); mapWrapper.SetPixel(u, v, color); mapWrapper_rev.SetPixel(191 - u, v, color); //mapWrapper.SetPixel(new Point(u,v), Util.addHue(bitmapWrapper.GetPixel(new Point(192*x+u, 192*y+v)), hue)); // map.SetPixel(u, v, Util.addHue(bitmap.GetPixel(192 * x + u, 192 * y + v), hue)); } } mapWrapper.UnWrapper(); mapWrapper_rev.UnWrapper(); bitmaps.Add(map); bitmap_revs.Add(map_rev); } } bitmapWrapper.UnWrapper(); return(true); }