0

我想在屏幕上绘制一个加载圆(.gif),而应用程序正在加载一些大的东西。但是我不能以我想要的速度跑圈,因为我有记忆问题。有谁知道如何解决这些问题(在 75 毫秒而不是 1000 毫秒)?以及如何在完成后删除圆圈(它不再消失)。

编辑:当窗口大小改变时执行加载函数。

    public Main()
    {
        InitializeComponent();
        StartUp();
        WindowState = FormWindowState.Maximized;
    }

    bool onrun;
    bool done;
    System.Threading.Timer timer;
    GifImage Circle;
    Point center;

    void StartUp()
    {
        onrun = true;
        done = false;
        timer = new System.Threading.Timer(new System.Threading.TimerCallback(Animate));
        timer.Change(0, 500);
    }

    void Animate(object sender)
    {
        if (onrun == true)
        {
            Circle = new GifImage("circleAnim.gif");
            Circle.ReverseAtEnd = false;
            int width = Screen.PrimaryScreen.Bounds.Width;
            int height = Screen.PrimaryScreen.Bounds.Height;
            center = new Point((width / 2) - 150, (height / 2) - 150);
            onrun = false;
        }
        else if (done == true)
        {
            timer.Dispose();
        }

        Image i = Circle.GetNextFrame();
        System.Drawing.Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero);
        try
        {
            GraphicsObject.DrawImage(i, center);
            i.Dispose();
            GraphicsObject.Dispose();
        }
        catch { }
    }

    private void Load(object sender, EventArgs e)
    {
        int hduizend = 100000;

        for (int i = 1; i <= 100000000; i++)
        {
            hduizend /= 2;
            hduizend *= 2;
        }

        done = true;
    }

EDIT2(错误):“试图读取或写入受保护的内存。这通常表明其他内存已损坏”。

gif图像本身也不是问题:

public class GifImage
{
private Image gifImage;
private FrameDimension dimension;
private int frameCount;
private int currentFrame = -1;
private bool reverse;
private int step = 1;

public GifImage(string path)
{
    gifImage = Image.FromFile(path); 
    dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); 
    frameCount = gifImage.GetFrameCount(dimension); 
}

public bool ReverseAtEnd 
{
    get { return reverse; }
    set { reverse = value; }
}

public Image GetNextFrame()
{

    currentFrame += step;


    if (currentFrame >= frameCount || currentFrame < 1)
    {
        if (reverse)
        {
            step *= -1;
            currentFrame += step; 
        }
        else
            currentFrame = 0;
    }
    return GetFrame(currentFrame);
}

public Image GetFrame(int index)
{
    gifImage.SelectActiveFrame(dimension, index); 
    return (Image)gifImage.Clone(); 
}

}

4

1 回答 1

1

将 gif 分配给 PictureBox,并在需要时仅显示 Picturebox。它将处理动画。您只需要注意何时需要显示并定位它。

private PictureBox pictureBox = new PictureBox();
Image animatedPicture = Image.FromFile(path);

...
int width = Screen.PrimaryScreen.Bounds. 
int height = Screen.PrimaryScreen.Bounds.Height;
center = new Point((width / 2) - 150, (height / 2) - 150);
pictureBox.Location = center;
pictureBox.Visible = true;
于 2012-09-21T17:24:07.930 回答