ユーザーがボタンをクリックしたときにいくつかの計算を行い、picturebox ペイント イベントを呼び出して、計算結果に基づいて新しい BMP を描画する Winform プログラムがあります。これはうまくいきます。
今、私はこれを100回行いたいと思っています.pictureboxが更新されるたびに、以下のようにラベルのテキストを更新して、現在の反復を確認したいと思います:
private void button2_Click(object sender, EventArgs e)
{
for (int iterations = 1; iterations <= 100; iterations++)
{
// do some calculations to change the cellmap parameters
cellMap.Calculate();
// Refresh picturebox1
pictureBox1.Invalidate();
pictureBox1.Update();
// Update label with the current iteration number
label1.Text = iterations.ToString();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(cellMap.Dimensions.Width, cellMap.Dimensions.Height);
Graphics gBmp = Graphics.FromImage(bmp);
int rectWidth = scaleFactor;
int rectHeight = scaleFactor;
// Create solid brushes
Brush blueBrush = new SolidBrush(Color.Blue);
Brush greenBrush = new SolidBrush(Color.Green);
Brush transparentBrush = new SolidBrush(Color.Transparent);
Graphics g = e.Graphics;
for (int i = 0; i < cellMap.Dimensions.Width; i++)
{
for (int j = 0; j < cellMap.Dimensions.Height; j++)
{
// retrieve the rectangle and draw it
Brush whichBrush;
if (cellMap.GetCell(i, j).CurrentState == CellState.State1)
{
whichBrush = blueBrush;
}
else if (cellMap.GetCell(i, j).CurrentState == CellState.State2)
{
whichBrush = greenBrush;
}
else
{
whichBrush = transparentBrush;
}
// draw rectangle to bmp
gBmp.FillRectangle(whichBrush, i, j, 1f, 1f);
}
}
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
私が抱えている問題は、最後のピクチャボックスの更新が完了した後にのみラベルテキストが表示されることです。基本的に、1 から 99 までは表示されません。繰り返しごとに BMP が変化するため、リフレッシュのたびにピクチャボックスが更新されるのがわかります。何か案が?