Sunteți pe pagina 1din 4

Almacenar y Mostrar Imgenes [Vb.

Net & SQL SERVER]


Publicado el 24 octubre, 2011 por Luis Bautista
Hola De Nuevo ahora haremos un ejemplo simple Para Almacenar imgenes en nuestro servidor de base de datos Desde un formulario en el lenguaje desde visual basic.net Para Esto Utilizaremos : 4 Button o o o o o o o Un Button Para Buscar La Imagen Un Button Para Guardar La Imagen En La Base De Datos Un Button Para Visualizar Las Imgenes Guardadas En La Base De Datos Un Button Para Ocultar Las Imgenes Almacenadas En La Base De Datos Un OpenFileDialog Para Seleccionar Nuestra Imagen A Almacenar Un DataGridView Para Mostrarnos los datos De La Base De Datos Un PictureBox Donde Nos Mostrara Y Nos Dara Una vista Previa De las Imgenes. 1 Label o Un Label Para Mostrarlos La Ruta Donde se encuentra la imagen

1 OpenFileDialog1 1 DataGridView 1 PictureBox

Bueno Con Estos Controles Crearemos Un Diseo Algo As.

Y Bueno Ahora Para El Ejemplo Crearemos Una Base De Datos Para Este Ejemplo:

Create Database TechPeru Go Use TechPeru Go Create Table AlmacenarImagenes( Id int primary key identity(0,1), Imagen Image ) Ahora El Codigo Fuente Imports System.Data Imports System.Data.SqlClient Imports System.IO Public Class Form1 Dim Str As String = "Data Source=.;Initial Catalog=TechPeru;Integrated Security=True" Dim Da As New SqlDataAdapter Dim Dt As DataTable Dim Cn As New SqlConnection(Str) Dim Cmd As New SqlCommand Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Size = New System.Drawing.Size(396, 412) DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect OpenFileDialog1.Filter = "Todos(*.Jpg, *.Png, *.Gif, *.Tiff, *.Jpeg, *.Bmp)|*.Jpg; *.Png; *.Gif; *.Tiff; *.Jpeg; *.Bmp"

End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Size = New System.Drawing.Size(396, 412) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Size = New System.Drawing.Size(396, 591) Try Cn.Open() With Cmd .CommandType = CommandType.Text .CommandText = "SELECT * from AlmacenarImagenes" .Connection = Cn End With With Da .SelectCommand = Cmd Dt = New DataTable .Fill(Dt) DataGridView1.DataSource = Dt End With Catch ex As Exception MsgBox(ex.Message) Finally Cn.Close() End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click OpenFileDialog1.ShowDialog() Label1.Text = OpenFileDialog1.FileName.ToString PictureBox1.Image = System.Drawing.Image.FromFile(Label1.Text) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try Cn.Open() Dim arrFilename() As String = Split(Text, "\") Array.Reverse(arrFilename) Dim ms As New MemoryStream PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat) Dim arrImage() As Byte = ms.GetBuffer With Cmd .CommandType = CommandType.Text .CommandText = "Insert Into AlmacenarImagenes(Imagen)Values(@Imagen)" .Connection = Cn .Parameters.Add(New SqlParameter("@Imagen", SqlDbType.Image)).Value = arrImage End With MessageBox.Show("Registrado Correctamente") Cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message) Finally Cmd.Parameters.Clear() Cn.Close() End Try End Sub Function ExtraerImagen(ByVal Foto As Integer) As Byte() With Cmd .CommandType = CommandType.Text

.CommandText = "Select Imagen From AlmacenarImagenes Where id = " & Foto .Connection = Cn End With With Cn .Open() Dim MyPhoto() As Byte = CType(Cmd.ExecuteScalar(), Byte()) .Close() Return MyPhoto End With End Function Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick Try Dim ms As New MemoryStream(ExtraerImagen(CInt(DataGridView1.SelectedCells(0).Value)) ) PictureBox1.Image = Image.FromStream(ms) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class