elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Manejo de Picturebox con un Timer en C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Manejo de Picturebox con un Timer en C#  (Leído 4,118 veces)
romybe

Desconectado Desconectado

Mensajes: 7



Ver Perfil
Manejo de Picturebox con un Timer en C#
« en: 16 Noviembre 2014, 01:11 am »

Hola!

Tengo que hacer una aplicación tipo álbum de fotos, donde el usuario ingrese la cantidad de segundos de intervalo y al clickear en el botón "comenzar" dichas imágenes comiencen a pasarse en el picturebox y cuando se llegue a la última imagen se vuelva a la primera.

Como dije debo utilizar un timer, pero estoy confusa en cómo tratar las imágenes... Había pensado en un arreglo pero nunca hice uno con imágenes y no sé cómo.
Si me pueden tirar ideas o ayudar con algo!
Muchas gracias!


En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.818



Ver Perfil
Re: Manejo de Picturebox con un Timer en C#
« Respuesta #1 en: 16 Noviembre 2014, 14:05 pm »

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:
Código
  1. Public Class Form1
  2.  
  3.    ' instancio unas imagenes por defecto para este ejemplo...
  4.    Private ReadOnly img1 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\001.jpg")
  5.    Private ReadOnly img2 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\002.jpg")
  6.    Private ReadOnly img3 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\003.jpg")
  7.    Private ReadOnly img4 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\004.jpg")
  8.    Private ReadOnly img5 As Image = Image.FromFile("D:\Customización\Wallpapers\_Favoritos\005.jpg")
  9.  
  10.    Private WithEvents imgListPhotos As New List(Of Image) From {img1, img2, img3, img4, img5}
  11.    Private WithEvents pcbPhotos As New PictureBox
  12.    Private WithEvents timerPhotos As New Timer
  13.  
  14.    Private imgInterval As Integer = 2000I
  15.  
  16.    ''' <summary>
  17.    ''' Initializes a new instance of the <see cref="Form1"/> class.
  18.    ''' </summary>
  19.    Public Sub New()
  20.  
  21.        ' This call is required by the designer.
  22.        InitializeComponent()
  23.  
  24.        ' Add any initialization after the InitializeComponent() call.
  25.        Me.Controls.Add(Me.pcbPhotos)
  26.  
  27.        With Me.pcbPhotos
  28.            .Dock = DockStyle.Fill
  29.            .BackgroundImageLayout = ImageLayout.Stretch
  30.        End With
  31.  
  32.        With Me.timerPhotos
  33.            .Interval = Me.imgInterval
  34.            .Enabled = True
  35.            .Start()
  36.        End With
  37.  
  38.    End Sub
  39.  
  40.    ''' <summary>
  41.    ''' Handles the Tick event of the timerPhotos control.
  42.    ''' </summary>
  43.    ''' <param name="sender">The source of the event.</param>
  44.    ''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  45.    Private Sub TimerPhotos_Tick(ByVal sender As Object, ByVal e As EventArgs) _
  46.    Handles timerPhotos.Tick
  47.  
  48.        With Me.pcbPhotos
  49.  
  50.            If .BackgroundImage Is Nothing Then
  51.                .BackgroundImage = Me.imgListPhotos.First
  52.  
  53.            Else
  54.                Dim imgIndex As Integer =
  55.                    Me.imgListPhotos.FindIndex(Function(img As Image) img.Equals(.BackgroundImage)) _
  56.                    + 1I
  57.  
  58.                If imgIndex = Me.imgListPhotos.Count Then ' RollBack
  59.                    imgIndex = 0I
  60.                End If
  61.  
  62.                .BackgroundImage = Me.imgListPhotos(imgIndex)
  63.  
  64.            End If ' currentImg Is Nothing
  65.  
  66.        End With ' Me.pcbPhotos
  67.  
  68.    End Sub
  69.  
  70. End Class

C# (conversión online):
Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. public class Form1
  8. {
  9.  
  10. // instancio unas imagenes por defecto para este ejemplo...
  11. private readonly Image img1 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\001.jpg");
  12. private readonly Image img2 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\002.jpg");
  13. private readonly Image img3 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\003.jpg");
  14. private readonly Image img4 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\004.jpg");
  15. private readonly Image img5 = Image.FromFile("D:\\Customización\\Wallpapers\\_Favoritos\\005.jpg");
  16.  
  17. private List<Image> imgListPhotos = new List<Image> {
  18. img1,
  19. img2,
  20. img3,
  21. img4,
  22. img5
  23. };
  24.  
  25. private PictureBox pcbPhotos = new PictureBox();
  26. private Timer withEventsField_timerPhotos = new Timer();
  27. private Timer timerPhotos {
  28. get { return withEventsField_timerPhotos; }
  29. set {
  30. if (withEventsField_timerPhotos != null) {
  31. withEventsField_timerPhotos.Tick -= TimerPhotos_Tick;
  32. }
  33. withEventsField_timerPhotos = value;
  34. if (withEventsField_timerPhotos != null) {
  35. withEventsField_timerPhotos.Tick += TimerPhotos_Tick;
  36. }
  37. }
  38.  
  39. }
  40.  
  41. private int imgInterval = 2000;
  42.  
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="Form1"/> class.
  45. /// </summary>
  46. public Form1()
  47. {
  48. // This call is required by the designer.
  49. InitializeComponent();
  50.  
  51. // Add any initialization after the InitializeComponent() call.
  52. this.Controls.Add(this.pcbPhotos);
  53.  
  54. this.pcbPhotos.Dock = DockStyle.Fill;
  55. this.pcbPhotos.BackgroundImageLayout = ImageLayout.Stretch;
  56.  
  57. this.timerPhotos.Interval = this.imgInterval;
  58. this.timerPhotos.Enabled = true;
  59. this.timerPhotos.Start();
  60.  
  61. }
  62.  
  63. /// <summary>
  64. /// Handles the Tick event of the timerPhotos control.
  65. /// </summary>
  66. /// <param name="sender">The source of the event.</param>
  67. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  68. private void TimerPhotos_Tick(object sender, EventArgs e)
  69. {
  70.  
  71. if (this.pcbPhotos.BackgroundImage == null) {
  72. this.pcbPhotos.BackgroundImage = this.imgListPhotos.First;
  73.  
  74. } else {
  75. int imgIndex = this.imgListPhotos.FindIndex((Image img) => img.Equals(this.pcbPhotos.BackgroundImage)) + 1;
  76.  
  77. // RollBack
  78. if (imgIndex == this.imgListPhotos.Count) {
  79. imgIndex = 0;
  80. }
  81.  
  82. this.pcbPhotos.BackgroundImage = this.imgListPhotos(imgIndex);
  83.  
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. //=======================================================
  91. //Service provided by Telerik (www.telerik.com)
  92. //Conversion powered by NRefactory.
  93. //=======================================================

Saludos


« Última modificación: 16 Noviembre 2014, 15:19 pm por Eleкtro » En línea

romybe

Desconectado Desconectado

Mensajes: 7



Ver Perfil
Re: Manejo de Picturebox con un Timer en C#
« Respuesta #2 en: 5 Diciembre 2014, 01:34 am »

Al final lo hice más sencillo:

Código
  1. int cont = 0;
  2.  
  3.        private void timer1_Tick(object sender, EventArgs e)
  4.        {
  5.            cont++;
  6.  
  7.            switch (cont)
  8.            {
  9.                case 1:
  10.                    pbImagenes.Image = Properties.Resources.foto1;
  11.                    break;
  12.                case 2:
  13.                    pbImagenes.Image = Properties.Resources.foto2;
  14.                    break;
  15.                case 3:
  16.                    pbImagenes.Image = Properties.Resources.foto3;
  17.                    break;
  18.                case 4:
  19.                    pbImagenes.Image = Properties.Resources.foto4;
  20.                    break;
  21.                case 5:
  22.                    pbImagenes.Image = Properties.Resources.foto5;
  23.                    break;
  24.                case 6:
  25.                    pbImagenes.Image = Properties.Resources.foto6;
  26.                    break;
  27.                case 7:
  28.                    pbImagenes.Image = Properties.Resources.foto7;
  29.                    break;
  30.                case 8:
  31.                    pbImagenes.Image = Properties.Resources.foto8;
  32.                    break;
  33.                case 9:
  34.                    pbImagenes.Image = Properties.Resources.foto9;
  35.                    break;
  36.                default:
  37.                    cont = 0;
  38.                    break;
  39.            }
  40.        }
  41.  
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
AYUDA CON EL PICTUREBOX
Programación Visual Basic
IvanUgu 3 2,236 Último mensaje 10 Julio 2005, 02:51 am
por IvanUgu
PictureBox
Programación Visual Basic
CsarGR 3 3,020 Último mensaje 15 Diciembre 2005, 23:34 pm
por NYlOn
Tutos: obtener datos y manejo de windows con c#... tuto_1: manejo de procesos
Scripting
tongoxcore 2 9,060 Último mensaje 21 Julio 2008, 23:44 pm
por Zaraki_lkenpachi
Problema con modificación de un PictureBox desde el hilo generado por un Timer.
.NET (C#, VB.NET, ASP)
SARGE553413 4 4,533 Último mensaje 7 Octubre 2014, 20:37 pm
por Eleкtro
Picturebox C#
.NET (C#, VB.NET, ASP)
MHMC777 4 9,253 Último mensaje 10 Noviembre 2014, 19:29 pm
por Eleкtro
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines