' VISOR DE IMAGENES EN
VISUAL BASIC .NET
' Miliuco - 03/08/2003
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 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 mnSalir2 As
System.Windows.Forms.MenuItem
Friend WithEvents lbImg2 As
System.Windows.Forms.Label
Friend WithEvents MenuItem3 As
System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()>
Private Sub
InitializeComponent()
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.mnCode
= 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
Me.SuspendLayout()
'
'pbxImg
'
Me.pbxImg.BorderStyle
= System.Windows.Forms.BorderStyle.Fixed3D
Me.pbxImg.Location
= New System.Drawing.Point(64, 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.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
'
'dlgSave
'
Me.dlgSave.FileName
= "doc1"
'
'VScrollBar1
'
Me.VScrollBar1.Enabled
= False
Me.VScrollBar1.Location
= New System.Drawing.Point(32, 16)
Me.VScrollBar1.Maximum
= 107
Me.VScrollBar1.Minimum
= 10
Me.VScrollBar1.Name
= "VScrollBar1"
Me.VScrollBar1.Size
= New System.Drawing.Size(16, 304)
Me.VScrollBar1.TabIndex
= 10
Me.VScrollBar1.Value
= 100
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2,
Me.mnCode, Me.MenuItem3,
Me.mnSalir2})
'
'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º"
'
'mnCode
'
Me.mnCode.Index
= 2
Me.mnCode.Text
= "Ver código en VB .NET"
'
'MenuItem3
'
Me.MenuItem3.Index
= 3
Me.MenuItem3.Text = "Acerca
de..."
'
'mnSalir2
'
Me.mnSalir2.Index
= 4
Me.mnSalir2.Text
= "Salir del programa"
'
'lbImg2
'
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
'
'Form1
'
Me.AutoScaleBaseSize
= New System.Drawing.Size(5, 13)
Me.ClientSize
= New System.Drawing.Size(560, 374)
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 - Miliuco"
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 = "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
'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
nombreImg = dlgFile.FileName
pbxImg.Image = Image.FromFile(nombreImg)
pbxImg.SizeMode =
PictureBoxSizeMode.StretchImage
lbImg.Text = nombreImg
VScrollBar1.Enabled = True
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 = "Ninguna imagen cargada"
lbImg2.Text = ""
VScrollBar1.Enabled = False
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
Private Sub
mnSalir_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
mnSalir.Click
End
End Sub
'Salir
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 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
End Class