View Single Post
Old 06-06-2014, 01:47 AM
boops boops's Avatar
boops boops boops boops is offline
Join Date: Dec 2006
Location: Holland and France
Posts: 146

BackgroundImage doesn't animate a GIF anyway -- only the PictureBox.Image does that. But we can get round this using 2 picture boxes, one to play the animated GIF and another to show the tiled background image.

Here's an example. Start with a Form with a picture box (PictureBox1) for showing the tiled image. Don't set the BackgroundImage yet, but set its BackgroundImageLayout to Tile. You can set its Image to whatever you like.

Add a second picture box (PictureBox2) as a "player" for the animated GIF. Set its Image to the animated GIF and its SizeMode to AutoSize.

Add a Timer (Timer1) and set its Interval to 30 or so. The Timer will be used to grab the image from the player, using the DrawToBitmap method. An advantage of DrawToBitmap is that you can hide the source control off-screen or behind another control at runtime while grabbing (but don't use Hide).

Here's the code for the form: Code:
Dim tile As Bitmap Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load tile = New Bitmap(PictureBox2.Image.Width, PictureBox2.Image.Height) PictureBox2.Location = New Point(-PictureBox2.Width, -PictureBox2.Height) 'hide PictureBox2 off-screen Timer1.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick PictureBox2.DrawToBitmap(tile, PictureBox2.ClientRectangle) PictureBox1.BackgroundImage = tile PictureBox1.Refresh() End Sub

This worked for me with animated GIFs (attached below) for both the tiled BackgroundImage and the Image. The world turns against a chorus of dancing mice.
Attached Images
File Type: gif mouse.gif (13.6 KB, 22 views)
File Type: gif world2.gif (41.0 KB, 22 views)
File Type: gif mouseWorld2.gif (635.4 KB, 12 views)

Last edited by boops boops; 06-06-2014 at 02:20 AM.
Reply With Quote