jueves, 26 de mayo de 2011

Abrir, guardar y borrar una imagen


En el form colocar un control picturebox, tres botones y dos cajas de dialogo(De abrir y guardar respectivamente)


Codigo: 
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo ErrorHandler
        With OpenFileDialog1
            .Filter = "Archivo Jpg|*.Jpg|Archivo Bmp|*.Bmp"
            .FilterIndex = 1
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(.FileName)
            End If
        End With
        Exit Sub
ErrorHandler:
        MsgBox("Se produjo en error" & Chr(13) & "No se pudo leer el archivo: " + OpenFileDialog1.FileName, MsgBoxStyle.Information, "Aviso")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        On Error GoTo ErrorHandler
        With SaveFileDialog1
            .Title = "Guardar iamgen"
            .Filter = "Archivo Jpg|*.Jpg|Archivo Bmp|*.Bmp"
            .FilterIndex = 1
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image.Save(.FileName)
            End If
        End With
        Exit Sub
ErrorHandler:
        MsgBox("Se produjo en error" & Chr(13) & "No se pudo guardar la imagen", MsgBoxStyle.Information, "Aviso")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PictureBox1.Image = Nothing
        Me.Refresh()
    End Sub
End Class

Mover un control picturebox con el mouse

                 
En el form colocar un control picturebox y cargarle una imagen cualquiera y una caja de dialogo de abrir.
Codigo:

Public Class Form1
    Dim izquierda As Integer
    Dim alto As Integer


Código para cargar una imagen
 Private Sub pic_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles pic.DoubleClick
        On Error GoTo ErrorHandler
        If open.ShowDialog = Windows.Forms.DialogResult.OK Then pic.Image =      Image.FromFile(open.FileName)
        Exit Sub
ErrorHandler:
        MsgBox("Se ha generado un error" & Chr(13) & "No se pudo leer el archivo: " + open.SafeFileName, MsgBoxStyle.Information, "Aviso")
        Resume Next
    End Sub

    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseDown
        izquierda = e.X
        alto = e.Y
    End Sub

    Private Sub pic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Right Then
            pic.Location = New Point(pic.Left + e.X - izquierda, pic.Top + e.Y - alto)
        End If
    End Sub
End Class

Crear controles en tiempo de ejecucion

Para crear controles en tiempo de ejecución en visual net 2010 lo hacemos de la siguiente manera, en este caso vamos a crear controles picturebox.
Cargamos un form y le agregamos un control button1.
Codigo:



Public Class Form1
    Dim a As Integer


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Generamos el picturebox y le damos algunas propiedades básicas
        For i As Integer = 0 To 3
            Dim pic As New PictureBox
            With pic
                .Width = 150
                .Height = 100
                .Visible = True
                .BackColor = Color.White
                .SizeMode = PictureBoxSizeMode.StretchImage
                .Location = New Point(30, 10 + ((.Top + .Height + 10) * i))
            End With
            'cargamos los controles
            Controls.Add(pic)


            Agregamos unas imágenes para q se vean mejor 
            If a = 0 Then
                pic.Image = Image.FromFile("C:\Users\She\Desktop\te.png")
            End If
            If a = 1 Then
                pic.Image = Image.FromFile("C:\Users\She\Desktop\th.jpg")
            End If
            If a = 2 Then
                pic.Image = Image.FromFile("C:\Users\She\Desktop\Sil.jpg")
            End If
            If a = 3 Then
                pic.Image = Image.FromFile("C:\Users\She\Desktop\tek.jpg")
            End If
            a = a + 1
        Next
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.CenterScreen
        a = 0
    End Sub
End Class