Había pensado en un arreglo pero nunca hice uno con imágenes y no sé cómo.
Un
Array/Colección se puede crear de la misma manera para cualquier tipo de objeto, no tiene mucho misterio solo tienes que asignarle objetos de tipo
Bitmap o
Image cómo lo harías con enteros para un
Array de
Integer, por ejemplo.
Si las images no superan los
256x256 de resolución entonces te recomiendo utilizar un
ImageList, de lo contrario podrías utilizar una colección genérica de tipo
List(<T> Image).
Para el tema de volver a la primera imagen (es decir, el primer elemento de la colección) puedes llevar la cuenta del índice actual en una variable "contador", o bien puedes utilizar los métodos de búsqueda de la lista.
Ejemplo:
VB.Net:Public Class Form1
' instancio unas imagenes por defecto para este ejemplo...
Private ReadOnly img1 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\001.jpg")
Private ReadOnly img2 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\002.jpg")
Private ReadOnly img3 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\003.jpg")
Private ReadOnly img4 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\004.jpg")
Private ReadOnly img5 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\005.jpg")
Private WithEvents imgListPhotos As New List(Of Image) From {img1, img2, img3, img4, img5}
Private WithEvents pcbPhotos As New PictureBox
Private WithEvents timerPhotos As New Timer
Private imgInterval As Integer = 2000I
''' <summary>
''' Initializes a new instance of the <see cref="Form1"/> class.
''' </summary>
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Controls.Add(Me.pcbPhotos)
With Me.pcbPhotos
.Dock = DockStyle.Fill
.BackgroundImageLayout = ImageLayout.Stretch
End With
With Me.timerPhotos
.Interval = Me.imgInterval
.Enabled = True
.Start()
End With
End Sub
''' <summary>
''' Handles the Tick event of the timerPhotos control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub TimerPhotos_Tick(ByVal sender As Object, ByVal e As EventArgs) _
Handles timerPhotos.Tick
With Me.pcbPhotos
If .BackgroundImage Is Nothing Then
.BackgroundImage = Me.imgListPhotos.First
Else
Dim imgIndex As Integer =
Me.imgListPhotos.FindIndex(Function(img As Image) img.Equals(.BackgroundImage)) _
+ 1I
If imgIndex = Me.imgListPhotos.Count Then ' RollBack
imgIndex = 0I
End If
.BackgroundImage = Me.imgListPhotos(imgIndex)
End If ' currentImg Is Nothing
End With ' Me.pcbPhotos
End Sub
End Class
C# (conversión online):using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
// instancio unas imagenes por defecto para este ejemplo...
private readonly Image img1 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\001.jpg");
private readonly Image img2 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\002.jpg");
private readonly Image img3 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\003.jpg");
private readonly Image img4 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\004.jpg");
private readonly Image img5 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\005.jpg");
private List
<Image
> imgListPhotos
= new List
<Image
> { img1,
img2,
img3,
img4,
img5
};
private PictureBox pcbPhotos
= new PictureBox
(); private Timer withEventsField_timerPhotos
= new Timer
(); private Timer timerPhotos {
get { return withEventsField_timerPhotos; }
set {
if (withEventsField_timerPhotos != null) {
withEventsField_timerPhotos.Tick -= TimerPhotos_Tick;
}
withEventsField_timerPhotos = value;
if (withEventsField_timerPhotos != null) {
withEventsField_timerPhotos.Tick += TimerPhotos_Tick;
}
}
}
private int imgInterval = 2000;
/// <summary>
/// Initializes a new instance of the <see cref="Form1"/> class.
/// </summary>
public Form1()
{
// This call is required by the designer.
InitializeComponent();
// Add any initialization after the InitializeComponent() call.
this.Controls.Add(this.pcbPhotos);
this.pcbPhotos.Dock = DockStyle.Fill;
this.pcbPhotos.BackgroundImageLayout = ImageLayout.Stretch;
this.timerPhotos.Interval = this.imgInterval;
this.timerPhotos.Enabled = true;
this.timerPhotos.Start();
}
/// <summary>
/// Handles the Tick event of the timerPhotos control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void TimerPhotos_Tick(object sender, EventArgs e)
{
if (this.pcbPhotos.BackgroundImage == null) {
this.pcbPhotos.BackgroundImage = this.imgListPhotos.First;
} else {
int imgIndex = this.imgListPhotos.FindIndex((Image img) => img.Equals(this.pcbPhotos.BackgroundImage)) + 1;
// RollBack
if (imgIndex == this.imgListPhotos.Count) {
imgIndex = 0;
}
this.pcbPhotos.BackgroundImage = this.imgListPhotos(imgIndex);
}
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//=======================================================
Saludos