New product
Este producto ya no está disponible
Disponible el:

gif vb.net
Como manejar, pintar un gif animado en VB.NET. Sacar la velocidad de animación del gif, con una clase simple de aprender.Destinatario :
* Campos requeridos
o Cancelar
Las imágenes gif son muy utilizadas porque ofrecen sobre todo en Net framework de autogestión y amplio soporte.
Podemos colocar un gif directamente en un formulario vb.net para usarla como fondo del formulario o en un picturebox para señalar alguna espera o trabaja en segundo plano y automáticamente, NET hará el trabajo de animación sin escribir una sola línea de código.
¿Pero qué pasa si queremos manipular un gif de forma manual?
Tanto vb.net como c# ofrecen soporte para gif en las clases Bitmap, Imaging.FrameDimension.
A continuación, les muestro una clase sencilla que carga y maneja un gif:
Public Class ImageGif
Implements IDisposable
Private _Gif As Bitmap
Private _frameD As Imaging.FrameDimension
Private times() As Byte
Private _nFrames As Integer
Private _currentFrame As Integer
Private _fileName As String
Private _runGif As Boolean
Public Event UpdateGif(ByVal bmpGif As Bitmap)
Private WithEvents AutoUpdater As Timer
Public Sub OpenFile(ByVal filename As String, Optional ByVal autoUpdate As Boolean = True)
'Load the bitmap
_Gif = New Bitmap(filename)
_frameD = New Imaging.FrameDimension(_Gif.FrameDimensionsList(0))
_nFrames = _Gif.GetFrameCount(_frameD)
times = _Gif.GetPropertyItem(&H5100).Value
_runGif = autoUpdate
AutoUpdater = New Timer
AutoUpdater.Interval = 33
AutoUpdater.Enabled = True
End Sub
Public ReadOnly Property GifBitmap() As Bitmap
Get
Return _Gif
End Get
End Property
Public ReadOnly Property nFrames() As Integer
Get
Return _nFrames
End Get
End Property
Public Property currentFrame() As Integer
Get
Return _currentFrame
End Get
Set(ByVal value As Integer)
If value >= _nFrames Then value = 0
If value < 0 Then value = 0
_currentFrame = value
'update bitmap gif
_Gif.SelectActiveFrame(_frameD, _currentFrame)
End Set
End Property
Private Sub UpdateFrameGif() Handles AutoUpdater.Tick
Dim timeDelay As Integer
If _runGif Then
_currentFrame += 1
If _currentFrame >= _nFrames Then _currentFrame = 0
_Gif.SelectActiveFrame(_frameD, _currentFrame)
timeDelay = BitConverter.ToInt32(times, 4 * _currentFrame) * 10
RaiseEvent UpdateGif(_Gif)
AutoUpdater.Interval = timeDelay
End If
End Sub
#Region " IDisposable Support "
Private disposedValue As Boolean = False
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
AutoUpdater.Enabled = False
_Gif.Dispose()
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Es bastante simple de utilizar con muy pocos elementos:
Cargar la imagen gif e un formulario en vb.net sería:
1.- Declaramos el contenedor del gif,
2.- Cargamos la imagen en el contenedor. Para que no se vea el parpadeo, habilitamos el doblebuffer para el evento Paint.
3.- Interceptamos el evento updateGif para saber que el gif ha cambiado el frame e invalidamos el formulario para que se pinte.
4.- Pintamos el gif en el formulario con el evento Paint.
Es todo, con esto tendremos ya un gif animado en vb.net.
El código se muestra a continuación:
Private WithEvents Gif As ImageGif
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Gif = New ImageGif
Gif.OpenFile("C:ImagesGif1.gif")
Me.DoubleBuffered = True
End Sub
Private Sub Gif_UpdateGif(ByVal bmpGif As System.Drawing.Bitmap) Handles Gif.UpdateGif
Me.Invalidate()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(Gif.GifBitmap, New Point(0, 0))
End Sub
Bueno, seguiré mejorando esta clase para hacer una herramienta para extraer gif.
Si tienen dudas, abajo en el formulario de preguntas. : )
Realizado por:
Elimar G.
06/03/2019
Muy bien
Esto no existia en español! genial.
No se requiere registro
Si luego de leer nuestro articulo aun tiene dudas, puede hacer su pregunta utilizando el siguiente formulario.
|
Si el enlace de descarga redirige a otro producto que no es el descrito en el artículo o está roto, reportarlo usando nuestro