回答編集履歴
1
追記
test
CHANGED
@@ -33,3 +33,117 @@
|
|
33
33
|
pictureBox1.Invalidate(); // ←これを書かないと表示が更新されない
|
34
34
|
|
35
35
|
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
追記
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
連続する何らかのイベントに反応して重い処理をさせたい、ただし、間に合わない場合は端折ってもいい、というような処理をしたい場合は、私はだいたいこんなコードを書きます。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
ビットマップを2枚用意するのは、いわゆるダブルバッファリングというやつです。ただ、描画処理がすごく重くてビットマップの生成やガベージコレクションにかかる時間など無視できるなら、毎回生成してPictureBoxにセットする方法でも問題ないかもしれません。
|
50
|
+
|
51
|
+
```C#
|
52
|
+
|
53
|
+
private void Form1_Load(object sender, EventArgs e)
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
:
|
58
|
+
|
59
|
+
:
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
doubleBuffer[0] = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
|
64
|
+
|
65
|
+
doubleBuffer[1] = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
|
66
|
+
|
67
|
+
showIndex = 0;
|
68
|
+
|
69
|
+
backgroundWorker1.RunWorkerAsync();
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
private void numericUpDown1_Changed(object sender, EventArgs e)
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
lastLength = Convert.ToInt32(numericUpDown1.Value);
|
82
|
+
|
83
|
+
changeNotify.Set();
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
private int lastLength;
|
90
|
+
|
91
|
+
private AutoResetEvent changeNotify = new AutoResetEvent(false);
|
92
|
+
|
93
|
+
private Bitmap[] doubleBuffer = new Bitmap[2];
|
94
|
+
|
95
|
+
private int showIndex;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
while(true)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
changeNotify.WaitOne();
|
108
|
+
|
109
|
+
if(backgroundWorker1.CancellationPending )
|
110
|
+
|
111
|
+
break;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
int length = lastLength;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// 表示中でない方のビットマップに対して描画処理を行う
|
120
|
+
|
121
|
+
int drawIndex = showIndex ^ 1;
|
122
|
+
|
123
|
+
var drawBuffer = doubleBuffer[drawIndex];
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// 重い描画処理
|
128
|
+
|
129
|
+
:
|
130
|
+
|
131
|
+
:
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// 描画したビットマップをPictureBoxに設定して表示を更新
|
136
|
+
|
137
|
+
pictureBox1.Image = drawBuffer;
|
138
|
+
|
139
|
+
showIndex = drawIndex;
|
140
|
+
|
141
|
+
this.Invoke(new Action(pictureBox1.Invalidate));
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
|