' VISOR DE IMAGENES EN VISUAL BASIC .NET
' Miliuco - 03/08/2003
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Icon
Imports System.Drawing.IconConverter
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 VScrollBar1 As System.Windows.Forms.VScrollBar
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 mnCode As System.Windows.Forms.MenuItem
Friend WithEvents lbImg2 As System.Windows.Forms.Label
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents lbValue As System.Windows.Forms.Label
Friend WithEvents btMax As System.Windows.Forms.Button
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(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.VScrollBar1 = New System.Windows.Forms.VScrollBar
Me.MainMenu1 = New System.Windows.Forms.MainMenu
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.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.mnCode = New System.Windows.Forms.MenuItem
Me.lbImg2 = New System.Windows.Forms.Label
Me.lbValue = New System.Windows.Forms.Label
Me.btMax = New System.Windows.Forms.Button
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
'
'pbxImg
'
Me.pbxImg.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.pbxImg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.pbxImg.Location = New System.Drawing.Point(56, 16)
Me.pbxImg.Name = "pbxImg"
Me.pbxImg.Size = New System.Drawing.Size(456, 304)
Me.pbxImg.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.pbxImg.TabIndex = 0
Me.pbxImg.TabStop = False
'
'lbImg
'
Me.lbImg.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lbImg.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.lbImg.ForeColor = System.Drawing.SystemColors.ControlText
Me.lbImg.Location = New System.Drawing.Point(8, 328)
Me.lbImg.Name = "lbImg"
Me.lbImg.Size = New System.Drawing.Size(536, 16)
Me.lbImg.TabIndex = 11
Me.lbImg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'VScrollBar1
'
Me.VScrollBar1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.VScrollBar1.Enabled = False
Me.VScrollBar1.Location = New System.Drawing.Point(16, 24)
Me.VScrollBar1.Minimum = 10
Me.VScrollBar1.Name = "VScrollBar1"
Me.VScrollBar1.Size = New System.Drawing.Size(16, 288)
Me.VScrollBar1.TabIndex = 10
Me.VScrollBar1.Value = 100
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.MenuItem4})
'
'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.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 = "Tamaño real"
'
'mnAjustar
'
Me.mnAjustar.Index = 2
Me.mnAjustar.Text = "Ajustar a la ventana"
'
'mnFlip
'
Me.mnFlip.Index = 3
Me.mnFlip.Text = "Voltear horizontalmente"
'
'mnFlip2
'
Me.mnFlip2.Index = 4
Me.mnFlip2.Text = "Voltear verticalmente"
'
'mnGirar
'
Me.mnGirar.Index = 5
Me.mnGirar.Text = "Girar la imagen 180º"
'
'MenuItem3
'
Me.MenuItem3.Index = 1
Me.MenuItem3.Text = "Acerca de..."
'
'mnCode
'
Me.mnCode.Index = 0
Me.mnCode.Text = "Ver código"
'
'lbImg2
'
Me.lbImg2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lbImg2.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.lbImg2.ForeColor = System.Drawing.SystemColors.ControlText
Me.lbImg2.Location = New System.Drawing.Point(8, 352)
Me.lbImg2.Name = "lbImg2"
Me.lbImg2.Size = New System.Drawing.Size(536, 16)
Me.lbImg2.TabIndex = 12
Me.lbImg2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lbValue
'
Me.lbValue.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lbValue.BackColor = System.Drawing.Color.Gainsboro
Me.lbValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.lbValue.Enabled = False
Me.lbValue.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.lbValue.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lbValue.Location = New System.Drawing.Point(456, 288)
Me.lbValue.Name = "lbValue"
Me.lbValue.Size = New System.Drawing.Size(48, 24)
Me.lbValue.TabIndex = 14
Me.lbValue.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.ToolTip1.SetToolTip(Me.lbValue, "Valor actual de la barra de Scroll")
'
'btMax
'
Me.btMax.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btMax.BackColor = System.Drawing.Color.Gainsboro
Me.btMax.Enabled = False
Me.btMax.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btMax.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btMax.Location = New System.Drawing.Point(400, 288)
Me.btMax.Name = "btMax"
Me.btMax.Size = New System.Drawing.Size(48, 24)
Me.btMax.TabIndex = 15
Me.btMax.Text = "Max."
Me.ToolTip1.SetToolTip(Me.btMax, "Llevar la barra de Scroll al 100%")
'
'MenuItem4
'
Me.MenuItem4.Index = 2
Me.MenuItem4.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnCode, Me.MenuItem3})
Me.MenuItem4.Text = "Ayuda"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(552, 382)
Me.Controls.Add(Me.btMax)
Me.Controls.Add(Me.lbValue)
Me.Controls.Add(Me.lbImg2)
Me.Controls.Add(Me.VScrollBar1)
Me.Controls.Add(Me.lbImg)
Me.Controls.Add(Me.pbxImg)
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 imágenes"
Me.ResumeLayout(False)
End Sub
#End Region
' Variable para ser utilizada en el método mnAbrir_Click
Dim nombreImgAbrir As String = ""
' Variable para ser utilizada en el método mnGuardar_Click
Dim nombreImgGuardar 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 = "Ninguna imagen cargada"
lbImg2.Text = ""
End Sub
'Mover la barra de desplazamiento vertical
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
Me.lbValue.Text = Me.VScrollBar1.Value & " %"
'entero para almacenar el valor de la barra de desplazamiento
Dim imgZoom As Single = VScrollBar1.Value
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
'código para que la caja de imagen sea redimensionada
'manteniendo el modo de colocación de la imagen: centrada o ajustada
If (pbxImg.SizeMode = PictureBoxSizeMode.StretchImage) Then
pbxImg.Width = (456 * imgZoom) \ 100
pbxImg.Height = (304 * imgZoom) \ 100
End If
If (pbxImg.SizeMode = PictureBoxSizeMode.Normal) Then
pbxImg.Width = (456 * imgZoom) / 100
pbxImg.Height = (304 * imgZoom) / 100
End If
End If
'Recarga la imagen
pbxImg.Refresh()
End Sub
'Para abrir un archivo
Private Sub mnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnAbrir.Click
dlgFile = New OpenFileDialog
dlgFile.Filter = "Imágenes JPG (*.jpg)|*.jpg|Mapas de bits (*.bmp)|*.bmp"
dlgFile.Title = "Abre una imagen JPG o BMP"
If dlgFile.ShowDialog() = DialogResult.OK Then
nombreImgAbrir = dlgFile.FileName
pbxImg.Image = Image.FromFile(nombreImgAbrir)
If (Me.pbxImg.Image.Width < 456) Or (Me.pbxImg.Image.Height < 304) Then
pbxImg.SizeMode = PictureBoxSizeMode.Normal
Else
pbxImg.SizeMode = PictureBoxSizeMode.StretchImage
End If
lbImg.Text = nombreImgAbrir
VScrollBar1.Enabled = True
lbImg2.Text = "Ancho de imagen: " + pbxImg.Image.Width.ToString + _
" px. / Alto de imagen: " + pbxImg.Image.Height.ToString + " px"
End If
Me.btMax.Enabled = True
Me.lbValue.Text = Me.VScrollBar1.Value & " %"
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 = "Ninguna imagen cargada"
lbImg2.Text = ""
VScrollBar1.Enabled = False
Me.btMax.Enabled = False
Me.lbValue.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
Dim 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
nombreImgGuardar = dlgSave.FileName
If (nombreImgGuardar.EndsWith("jpg")) Then
pbxImg.Image.Save(nombreImgGuardar, ImageFormat.Jpeg)
ElseIf (nombreImgGuardar.EndsWith("bmp")) Then
pbxImg.Image.Save(nombreImgGuardar, ImageFormat.Bmp)
Try
Dim icono As Icon = New Icon(nombreImgGuardar)
pbxImg.Image.Save(nombreImgGuardar, ImageFormat.Icon)
Catch pollo As Exception
MessageBox.Show(pollo.Message, "Excepción")
End Try
End If
End If
End If
End Sub
'Salir
Private Sub mnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnSalir.Click
Close()
End Sub
'Salir
Private Sub mnSalir2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Close()
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 a tamaño real
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.Normal
End If
End Sub
'Para ajustar la imagen
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
'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
'Mostrar el código del programa
Private Sub mnCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnCode.Click
'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
'Caja de mensaje Acerca de...
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
MessageBox.Show("Visor de imágenes realizado en Visual Basic. Autor: MILIUCO. Fecha: 03/08/2003" _
+ vbCrLf + "El archivo ""Visor de imágenes.html"" debe estar en la misma carpeta que el ejecutable" _
+ " de la aplicación.", "Acerca de...", MessageBoxButtons.OK)
End Sub
'Poner el zoom de imagen al 100 por acción de usuario. El control ScrollBar
'no permite llevar el valor al Maximum de manera dinámica, lo más a que
'puede llegar es a Maximum - LargeChange. Sólo se puede alcanzar el
'Maximum mediante programación.
Private Sub btMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMax.Click
'para que solamente lo haga si hay imagen en la caja de imagen
If Not pbxImg.Image Is Nothing Then
Me.VScrollBar1.Value = 100
pbxImg.Width = 456
pbxImg.Height = 304
Me.lbValue.Text = Me.VScrollBar1.Value & " %"
'Recarga la imagen
pbxImg.Refresh()
End If
End Sub
End Class