' VISOR DE IMAGENES EN VISUAL BASIC .NET
' perez987 enero/2019
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Código generado por el Diseñador de Windows Forms "
Public Sub New()
MyBase.New()
'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()
'Agregar cualquier inicialización después de la llamada a InitializeComponent()
End Sub
'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requerido por el Diseñador de Windows Forms
Private components As System.ComponentModel.IContainer
'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
'Puede modificarse utilizando el Diseñador de Windows Forms.
'No lo modifique con el editor de código.
Friend WithEvents pbxImg As System.Windows.Forms.PictureBox
Friend WithEvents dlgFile As System.Windows.Forms.OpenFileDialog
Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Friend WithEvents lbImg As System.Windows.Forms.Label
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents mnAbrir As System.Windows.Forms.MenuItem
Friend WithEvents mnCerrar As System.Windows.Forms.MenuItem
Friend WithEvents mnGuardar As System.Windows.Forms.MenuItem
Friend WithEvents mnSalir As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents mnCopiar As System.Windows.Forms.MenuItem
Friend WithEvents mnReal As System.Windows.Forms.MenuItem
Friend WithEvents mnAjustar As System.Windows.Forms.MenuItem
Friend WithEvents mnFlip As System.Windows.Forms.MenuItem
Friend WithEvents mnFlip2 As System.Windows.Forms.MenuItem
Friend WithEvents mnGirar As System.Windows.Forms.MenuItem
Friend WithEvents mnSalir2 As System.Windows.Forms.MenuItem
Friend WithEvents lbImg2 As System.Windows.Forms.Label
Friend WithEvents MenuItem4 As MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.pbxImg = New System.Windows.Forms.PictureBox()
Me.lbImg = New System.Windows.Forms.Label()
Me.dlgFile = New System.Windows.Forms.OpenFileDialog()
Me.dlgSave = New System.Windows.Forms.SaveFileDialog()
Me.MainMenu1 = New System.Windows.Forms.MainMenu(Me.components)
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.mnAbrir = New System.Windows.Forms.MenuItem()
Me.mnCerrar = New System.Windows.Forms.MenuItem()
Me.mnGuardar = New System.Windows.Forms.MenuItem()
Me.mnSalir = New System.Windows.Forms.MenuItem()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
Me.mnCopiar = New System.Windows.Forms.MenuItem()
Me.mnReal = New System.Windows.Forms.MenuItem()
Me.mnAjustar = New System.Windows.Forms.MenuItem()
Me.MenuItem4 = New System.Windows.Forms.MenuItem()
Me.mnFlip = New System.Windows.Forms.MenuItem()
Me.mnFlip2 = New System.Windows.Forms.MenuItem()
Me.mnGirar = New System.Windows.Forms.MenuItem()
Me.MenuItem3 = New System.Windows.Forms.MenuItem()
Me.mnSalir2 = New System.Windows.Forms.MenuItem()
Me.lbImg2 = New System.Windows.Forms.Label()
CType(Me.pbxImg, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'pbxImg
'
Me.pbxImg.BackColor = System.Drawing.SystemColors.ControlLight
Me.pbxImg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.pbxImg.Location = New System.Drawing.Point(19, 133)
Me.pbxImg.Name = "pbxImg"
Me.pbxImg.Size = New System.Drawing.Size(1021, 545)
Me.pbxImg.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.pbxImg.TabIndex = 0
Me.pbxImg.TabStop = False
'
'lbImg
'
Me.lbImg.ForeColor = System.Drawing.SystemColors.ControlText
Me.lbImg.Location = New System.Drawing.Point(19, 28)
Me.lbImg.Name = "lbImg"
Me.lbImg.Size = New System.Drawing.Size(1021, 34)
Me.lbImg.TabIndex = 11
Me.lbImg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'dlgSave
'
Me.dlgSave.FileName = "doc1"
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.mnSalir2, Me.MenuItem3})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnAbrir, Me.mnCerrar, Me.mnGuardar, Me.mnSalir})
Me.MenuItem1.Text = "Menú archivo"
'
'mnAbrir
'
Me.mnAbrir.Index = 0
Me.mnAbrir.Text = "Abrir archivo BMP / JPG"
'
'mnCerrar
'
Me.mnCerrar.Index = 1
Me.mnCerrar.Text = "Cerrar archivo"
'
'mnGuardar
'
Me.mnGuardar.Index = 2
Me.mnGuardar.Text = "Guardar como..."
'
'mnSalir
'
Me.mnSalir.Index = 3
Me.mnSalir.Text = "Salir del programa"
'
'MenuItem2
'
Me.MenuItem2.Index = 1
Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnCopiar, Me.mnReal, Me.mnAjustar, Me.MenuItem4, Me.mnFlip, Me.mnFlip2, Me.mnGirar})
Me.MenuItem2.Text = "Menú edición"
'
'mnCopiar
'
Me.mnCopiar.Index = 0
Me.mnCopiar.Text = "Copiar al portapapeles"
'
'mnReal
'
Me.mnReal.Index = 1
Me.mnReal.Text = "Ajustada con zoom"
'
'mnAjustar
'
Me.mnAjustar.Index = 2
Me.mnAjustar.Text = "Ajustada al PictureBox"
'
'MenuItem4
'
Me.MenuItem4.Index = 3
Me.MenuItem4.Text = "Tamaño real de la imagen"
'
'mnFlip
'
Me.mnFlip.Index = 4
Me.mnFlip.Text = "Voltear horizontalmente"
'
'mnFlip2
'
Me.mnFlip2.Index = 5
Me.mnFlip2.Text = "Voltear verticalmente"
'
'mnGirar
'
Me.mnGirar.Index = 6
Me.mnGirar.Text = "Girar la imagen 180º"
'
'MenuItem3
'
Me.MenuItem3.Index = 3
Me.MenuItem3.Text = "Acerca de..."
'
'mnSalir2
'
Me.mnSalir2.Index = 2
Me.mnSalir2.Text = "Salir del programa"
'
'lbImg2
'
Me.lbImg2.ForeColor = System.Drawing.SystemColors.ControlText
Me.lbImg2.Location = New System.Drawing.Point(19, 76)
Me.lbImg2.Name = "lbImg2"
Me.lbImg2.Size = New System.Drawing.Size(1021, 32)
Me.lbImg2.TabIndex = 12
Me.lbImg2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(192.0!, 192.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
Me.ClientSize = New System.Drawing.Size(1065, 699)
Me.Controls.Add(Me.lbImg2)
Me.Controls.Add(Me.lbImg)
Me.Controls.Add(Me.pbxImg)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Visor de imagen"
CType(Me.pbxImg, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
' Variable para ser utilizada en el método btAbrir_Click
Dim nombreImg As String = ""
'Al cargar el formulario
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lbImg.Text = "No hay imagen en el PictureBox"
lbImg2.Text = ""
End Sub
'Para abrir un archivo; observar la construcción con With en OpenFileDialog
Private Sub MnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnAbrir.Click
'antigua manera de implementarlo
'dlgFile = New OpenFileDialog
'dlgFile.Filter = "Imágenes JPG (*.jpg)|*.jpg|Mapas de bits (*.bmp)|*.bmp"
'dlgFile.Title = "Abre una imagen JPG o BMP"
'nueva manera de implementarlo
dlgFile = New OpenFileDialog With {
.Filter = "Imágenes JPG (*.jpg)|*.jpg|Mapas de bits (*.bmp)|*.bmp",
.Title = "Abre una imagen JPG o BMP"
}
If dlgFile.ShowDialog() = DialogResult.OK Then
nombreImg = dlgFile.FileName
pbxImg.Image = Image.FromFile(nombreImg)
pbxImg.SizeMode = PictureBoxSizeMode.Zoom
lbImg.Text = nombreImg
lbImg2.Text = "Ancho de imagen: " + pbxImg.Image.Width.ToString +
" px. / Alto de imagen: " + pbxImg.Image.Height.ToString + " px"
End If
End Sub
'Vaciar la caja de imagen y la etiqueta de información
Private Sub MnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnCerrar.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.Image = Nothing
lbImg.Text = "No hay imagen en el PictureBox"
lbImg2.Text = ""
End If
End Sub
'Para guardar el archivo en otro formato (BMP a JPG, JPG a BMP)
Private Sub MnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnGuardar.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
dlgSave = New SaveFileDialog
dlgSave.Filter = "Mapas de bits (*.bmp)|*.bmp|Imágenes JPG (*.jpg)|*.jpg"
dlgSave.Title = "Guarda como imagen JPG o BMP"
If dlgSave.ShowDialog() = DialogResult.OK Then
nombreImg = dlgSave.FileName
If (nombreImg.EndsWith("jpg")) Then
pbxImg.Image.Save(nombreImg, ImageFormat.Jpeg)
End If
If (nombreImg.EndsWith("bmp")) Then
pbxImg.Image.Save(nombreImg, ImageFormat.Bmp)
End If
End If
End If
End Sub
'Salir desde el menú Archivo
Private Sub MnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnSalir.Click
End
End Sub
'Salir desde el menú Salir del programa
Private Sub MnSalir2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnSalir2.Click
End
End Sub
'Para copiar la imagen al protapapeles
Private Sub MnCopiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnCopiar.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
Call FnClipboard() 'función detallada en el siguiente bloque
End If
End Sub
'Función que devuelve un objeto desde el portapapeles.
'La clase ClipBoard deriva de la clase Object y tiene 3 miembros:
'constructor / GetDataObject / SetDataObject
Private Function FnClipboard() As Object
'Copiar imagen al portapeles
'SetDataObject lleva 2 parámetros (objeto, booleano)
'Si booleano es true, el objeto queda en el portapapeles
'después de haber cerrado la aplicación
Clipboard.SetDataObject(pbxImg.Image, True)
'Variable objClipboard para contener el portapeles
Dim objClipboard As IDataObject = Clipboard.GetDataObject()
'devuelve el portapapeles como mapa de bits
Return objClipboard.GetData(DataFormats.Bitmap)
End Function
'Para mostrar la imagen ajustada a la ventana en sus proporciones originales
Private Sub MnReal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnReal.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.SizeMode = PictureBoxSizeMode.Zoom
End If
End Sub
'Para ajustar la imagen al tamaño del PictureBox
Private Sub MnAjustar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnAjustar.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.SizeMode = PictureBoxSizeMode.StretchImage
End If
End Sub
'para mostrar la imagen a tamaño real
Private Sub MenuItem4_Click(sender As Object, e As EventArgs) Handles MenuItem4.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.SizeMode = PictureBoxSizeMode.CenterImage
End If
End Sub
'Menú que voltea horizontalmente la imagen
Private Sub MnFlip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnFlip.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
pbxImg.Refresh() 'Recarga la imagen
End If
End Sub
'Para girar la imagen 180 grados
Private Sub MnGirar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnGirar.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipX)
pbxImg.Refresh() 'Recarga la imagen
End If
End Sub
'Menú que voltea verticalmente la imagen
Private Sub MnFlip2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnFlip2.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
pbxImg.Refresh() 'Recarga la imagen
End If
End Sub
'Caja de mensaje Acerca de...
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
MessageBox.Show("Sencillo visor de imágenes realizado en Visual Basic .NET" + vbCrLf + "Autor: perez987. Fecha: enero/2019",
"Acerca de...", MessageBoxButtons.OK)
End Sub
'' CODIGO COMENTADO. SU FUNCIÓN ERA ABRIR UN ARCHIVO _
'' HTML MOSTRANDO EL CÓDIGO VB DEL PROGRAMA _
'' DESDE UN MENÚ
''Mostrar el código del programa
'Private Sub MnCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' 'Obtener la ruta a la carpeta desde la que se ha iniciado la aplicación,
' Dim stRuta As String = Application.StartupPath
' Try ' para detectar y manipular excepciones (errores del programa)
' 'ProcessStartInfo se utiliza para iniciar procesos
' Dim abrirDoc As ProcessStartInfo = New ProcessStartInfo
' 'Añadir a la ruta "\Visor de Imagen.html" para abrir el archivo
' abrirDoc.FileName = stRuta + "\Visor de imágenes.html"
' 'Información al usuario de la ruta obtenida en su sistema
' MessageBox.Show("La ruta a tu archivo ""Visor de imágenes.html"" parece ser:" _
' + vbCrLf + vbCrLf + abrirDoc.FileName, "Archivo Visor de imágenes.html",
' MessageBoxButtons.OK)
' 'modo de apertura de archivo (también puede ser edit, print...)
' abrirDoc.Verb = "open"
' 'abrir en ventana normal
' abrirDoc.WindowStyle = ProcessWindowStyle.Normal
' 'Abrir el archivo mediante su programa asociado en Windows
' Process.Start(abrirDoc)
' Catch a As Exception 'Objeto Exception de nombre a
' 'Mensaje relativo a la excepción lanzada
' MessageBox.Show("Se ha producido la siguiente excepción:" _
' + vbCrLf + vbCrLf + a.Message, "Mensaje de error")
' End Try
'End Sub
End Class