- 題名: フェードイン・アウトを更に速くするには?
- 日時: 2010/11/24 11:28:59
- ID: 27631
- この記事の返信元:
- (なし)
- この記事への返信:
- [27638] Re[1]: フェードイン・アウトを更に速くするには?2010/11/24 22:25:32
- ツリーを表示
■No27646に返信(家財さんの記事)
>> System.Threading.Thread.Sleep()
> ここを1から0.1、0.0000000000001に変えても大差はなく……。
そもそも、Sleep メソッドに小数は渡せません。
渡せる型は、整数型または TimeSpan型だけです。
修正の方向としては、そもそも Sleep を使わないようにすべきです。
PictureBox.CreateGraphics() の利用も避けた方が良いでしょう。
具体的にはこんな感じ。VB で書いてみました。
C# が良ければ、下記を参照してみてください。
http://bbs.wankuma.com/index.cgi?mode=al2&namber=50887&KLOG=86
Public Class Form1
Private img As Image
Private count As Integer
Private offset As Integer
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
img = Image.FromFile("C:\サンプル.jpg")
Timer1.Interval = 64 '←描画速度の切り替え
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, _
ByVal e As FormClosedEventArgs) Handles Me.FormClosed
If img IsNot Nothing Then
img.Dispose()
End If
End Sub
Private Sub PictureBox1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles PictureBox1.Click
offset = 1
count = 0
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, _
ByVal e As EventArgs) Handles Timer1.Tick
count += offset
PictureBox1.Refresh()
If count >= 10 Then
offset = -1
ElseIf count <= 0 Then
Timer1.Stop()
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As PaintEventArgs) Handles PictureBox1.Paint
If img IsNot Nothing Then
DrawFadedImage(e.Graphics, img, count * 0.1F)
End If
End Sub
Public Shared Sub DrawFadedImage(ByVal g As Graphics, _
ByVal img As Image, ByVal alpha As Single)
Using back As New Bitmap(img.Width, img.Height)
Using bg As Graphics = Graphics.FromImage(back)
bg.Clear(Color.White)
Dim cm As New System.Drawing.Imaging.ColorMatrix()
cm.Matrix00 = 1
cm.Matrix11 = 1
cm.Matrix22 = 1
cm.Matrix33 = alpha
cm.Matrix44 = 1
Dim ia As New System.Drawing.Imaging.ImageAttributes()
ia.SetColorMatrix(cm)
bg.DrawImage(img, _
New Rectangle(0, 0, img.Width, img.Height), _
0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, ia)
g.DrawImage(back, 0, 0)
End Using
End Using
End Sub
End Class
「Tips」にある「画像をフェードイン、フェードアウトする」を利用しているのですが、もう少し速くフェードさせる事は出来ないものでしょうか?
初心者であることに加え、画像処理にとことん弱いので自分で出来た部分は数値を弄くるぐらいのものでした。結果としては単に雑になったり、余計遅くなったりしただけで……。
困っています。宜しくお願い致します。