我會處理這個不同 - 它已經有一段時間,因爲我已經與WinForms的東西玩,但我可能會採取更多的控制權渲染圖像。
在這個例子中,我已經得到了所有圖像在垂直堆疊的一個源位圖,存儲在裝配的資源:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Timer timer;
private Bitmap dice;
private int[] currentValues = new int[6];
private Random random = new Random();
public Form1()
{
InitializeComponent();
this.timer = new Timer();
this.timer.Interval = 500;
this.timer.Tick += TimerOnTick;
this.dice = Properties.Resources.Dice;
}
private void TimerOnTick(object sender, EventArgs eventArgs)
{
for (var i = 0; i < currentValues.Length; i++)
{
this.currentValues[i] = this.random.Next(1, 7);
}
this.panel1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.timer.Enabled)
{
this.timer.Stop();
}
else
{
this.timer.Start();
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
if (this.timer.Enabled)
{
for (var i = 0; i < currentValues.Length; i++)
{
e.Graphics.DrawImage(this.dice, new Rectangle(i * 70, 0, 60, 60), 0, (currentValues[i] - 1) * 60, 60, 60, GraphicsUnit.Pixel);
}
}
}
}
}
源是在這裏,如果有幫助:http://sdrv.ms/Wx2Ets
你預緩存你正在切換的'Bitmap'對象?如果你每次從磁盤加載都會有輕微的延遲.. –
是的,我預先將'Form1_Load(...)''Bitmap'緩存到'List'中,[this method](http: /stackoverflow.com/a/3567824/752142) –
illagrenan
你的源圖像有多大?我無法想象在屏幕截圖中顯示的內容會有明顯延遲(假設源圖像實際上很小)。 –