首页 > 解决方案 > 如何为从资源文件夹中提取的 GIF 设置动画?

问题描述

“射手”子类

class Shooter : Box
{
    public bool goleft;
    public bool goright;



    public Shooter(float startx, float starty)
    {
        pic = resizeImage (Properties.Resources.shooter, new Size(50,50)); // resize image
        goleft = false;
        goright = false;
        x = startx;
        y = starty;

    }

这是它继承代码的主类。

class Box
{
    public Image pic;
    public float x;
    public float y;
    public int speed;

    public Box()
    {
        x = 0;
        y = 0;
        speed = 0;
    }
    // Image Resizing Code
    public static Image resizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }
    // image resizing code


    public void Draw(Graphics g)
    {
        g.DrawImage(pic, x, y);
    }
    // some code bellow that basically states the borders and hit boxes.
}

所以,是的,我只是想弄清楚如何为本质上由构造函数构建的 gif 设置动画……射击者出现了,我可以移动它,但问题是,它只是不旋转。希望大家能想办法。感谢:D

标签: c#.netwinforms

解决方案


您的 GIF 没有动画,因为您的Box班级根本不支持它。

如果要对图像进行动画处理,不能以Bitmap的形式打开,需要获取图像数据并手动做动画,或者使用PictureBox来显示图像。是如何手动执行此操作的示例。请注意,为了调整 GIF 的大小,您还需要逐帧进行。


推荐阅读