我想在屏幕上绘制一个加载圈(.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();
}
}
答案 0 :(得分: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;