En el formulario:
- 1 PictureBox
- 2 Button
Codigo:
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class
Form1
Public SELECCION As
String
Public D_TABLE As
DataTable
Public D_ADAPTER As
OleDbDataAdapter
Public SQL As String
Public ORDEN_BD As
OleDb.OleDbCommand
Public da As SqlDataAdapter
Public sExtencion As
String
Public SERVIDOR As
String = "NOMBRE DEL SERVIDOR"
Public BD As String = "NOMBRE DE LA BASE DE DATOS"
Public PERSONA As
String = "NOMBRE DEL USUARIO"
Public CONTRASEÑA As
String = "LA CONTRASEÑA"
Public CONEXION As
New SqlConnection("Data Source="
& SERVIDOR & ";" &
"Initial Catalog="
& BD & ";"
&
"User Id="
& PERSONA & ";Password=" & CONTRASEÑA & "")
Private Sub
Button1_Click(sender As
Object, e As EventArgs) Handles
Button1.Click
Try
Dim ms1 As New System.IO.MemoryStream() 'Creamos el
MemoryStream.
PictureBox1.BackgroundImage.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg) 'Salvamos el imagen
que tenomos cargada en el PictureBox en el MemoryStream.
SQL = "UPDATE
CONFIGURACION SET LOGO = (@LOGO) WHERE ID=1" 'Creamos el Query.
CONEXION.Close()
CONEXION.Open() 'Abrimos la Coenxion
de la base de datos.
Dim cmd As New SqlCommand(SQL, CONEXION) 'Creamos el comando.
cmd.Parameters.Add(New SqlParameter("@LOGO", ms1.GetBuffer())) 'Agregamos el
MemoryStream a los parametros para poderlos guardar en la base de datos.
cmd.ExecuteNonQuery() 'ejecutamos el query.
CONEXION.Close()
cmd = Nothing
ms1.Dispose()
ms1.Close()
CONEXION.Close()
'MsgBox("La imagen ha sido
registrada", MsgBoxStyle.Information)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'OBTENEMOS LA IMAGEN EN LA BASE DE DATOS Y LA
CARGAMOS EL PICTUREBOX
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim dt As New DataTable
SQL = "SELECT * FROM
CONFIGURACION WHERE ID=1"
da = New SqlDataAdapter(SQL, CONEXION)
CONEXION.Open()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim ms As New System.IO.MemoryStream() 'Creamos el
MemoryStream y poder cargar la imagen.
Dim imageBuffer() As Byte = CType(dt.Rows(0).Item("LOGO"), Byte()) 'Conbertimos la imagen
cargada en el datatable a Bytes.
ms = New System.IO.MemoryStream(imageBuffer) 'Cargamos el
MemoryStream con la imagen ya convertida en Bytes.
PictureBox1.BackgroundImage = Nothing 'Si existe una imagen
cargada en el PictureBox la borramos.
PictureBox1.BackgroundImage = (Image.FromStream(ms)) 'Cargamos la imagen al
PictureBox, Nota: Lo hacemos como .BackgroundImage pero igual lo podemos hacer
como .Image.
PictureBox1.BackgroundImageLayout
= ImageLayout.Stretch 'Ajustamos la imagen
al todo el PictureBox.
CONEXION.Close()
dt.Clear()
dt.Reset()
ms.Dispose()
ms.Close()
SQL = ""
End If
CONEXION.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
CONEXION.Close()
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'OBTENEMOS
LA IMAGEN EN LA BASE DE DATOS Y LA CARGAMOS EL PICTUREBOX
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Dim dt As New DataTable
SQL = "SELECT * FROM
CONFIGURACION WHERE ID = 1"
da = New SqlDataAdapter(SQL, CONEXION)
CONEXION.Open()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim ms As New System.IO.MemoryStream() 'Creamos el
MemoryStream y poder cargar la imagen.
Dim imageBuffer() As Byte = CType(dt.Rows(0).Item("LOGO"), Byte()) 'Conbertimos la imagen
cargada en el datatable a Bytes.
ms = New System.IO.MemoryStream(imageBuffer) 'Cargamos el
MemoryStream con la imagen ya convertida en Bytes.
PictureBox1.BackgroundImage = Nothing 'Si existe una imagen
cargada en el PictureBox la borramos.
PictureBox1.BackgroundImage = (Image.FromStream(ms)) 'Cargamos la imagen al
PictureBox, Nota: Lo hacemos como .BackgroundImage pero igual lo podemos hacer
como .Image.
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch 'Ajustamos la imagen al todo el
PictureBox.
CONEXION.Close()
dt.Clear()
dt.Reset()
ms.Dispose()
ms.Close()
SQL = ""
End If
CONEXION.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub
Button2_Click(sender As
Object, e As EventArgs) Handles
Button2.Click
Try
Dim openFileDialog1 As New
OpenFileDialog()
Dim dialogo As
New DialogResult
openFileDialog1.Filter = "Imagen (JPG,BMP,PNG)|*.JPG;*.BMP;*.PNG|All files
(*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
dialogo =
openFileDialog1.ShowDialog()
If (dialogo = DialogResult.OK) Then
Dim bmp As New Bitmap(Image.FromFile(openFileDialog1.FileName))
bmp =
bmp.GetThumbnailImage(480, 480, Nothing, IntPtr.Zero)
'Redimencionamos pixeles deimagen (Opcional).
PictureBox1.BackgroundImage = Nothing
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch 'Ajustamos la imagen a
todo el picturebox.
PictureBox1.BackgroundImage =
bmp 'Cargamos
la imagen al PictureBox.
'sExtencion =
System.IO.Path.GetExtension(openFileDialog1.FileName) 'Obtenemos la Extencion
de la imagen cargada.
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Si deseas incluir datos y mostrarlos en un Grid, aqui tienes un ejemplo:
En este ejemplo muestra una pantalla de una planilla de configuracion del sistema, usted puede adaptarla a su gusto y segun lo que requiera su sistema
- 1 PictureBox
- 2 Button
- 1 DataGridView
- Mas las cajas de textos segun sus requerimientos
Recuerda que en el Grid debe estar habilitado reordenacion de columnas:
Finalmente el diseño del formulario es de la siguiente manera:
Codigo:
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class
Form1
Public SELECCION As
String
Public D_TABLE As
DataTable
Public D_ADAPTER As
OleDbDataAdapter
Public SQL As String
Public ORDEN_BD As
OleDb.OleDbCommand
Public da As SqlDataAdapter
Public sExtencion As
String
Public SERVIDOR As String = "NOMBRE DEL SERVIDOR"
Public BD As String = "NOMBRE DE LA BASE DE DATOS"
Public PERSONA As String = "NOMBRE DEL USUARIO"
Public CONTRASEÑA As String = "LA CONTRASEÑA"
Public CONEXION As
New SqlConnection("Data Source="
& SERVIDOR & ";" &
"Initial Catalog="
& BD & ";"
&
"User Id="
& PERSONA & ";Password=" & CONTRASEÑA & "")
Public lCONEXION As
New OleDbConnection("Provider=sqloledb;"
&
"Data Source=" & SERVIDOR &
";" &
"Initial Catalog="
& BD & ";"
&
"User Id="
& PERSONA & ";Password=" & CONTRASEÑA & "")
Private Sub
Button1_Click(sender As
Object, e As EventArgs) Handles
Button1.Click
Try
lCONEXION.Open()
SQL = "UPDATE CONFIGURACION SET TITULO_PDF='" & TXT_TITULO_PDF1.Text &
"',TITULO_PDF2
='"
& TXT_TITULO_PDF2.Text &
"',DELIMITADOR_TXT='" &
TXT_DELIMITADOR.Text &
"',RUTA_PDF='" &
TXT_DIRECCION_PDF.Text &
"',RUTA_WORD ='"
& TXT_DIRECCION_WORD.Text &
"',RUTA_TXT='" &
TXT_DIRECCION.Text &
"' WHERE ID= 1"
ORDEN_BD = New OleDbCommand(SQL,
lCONEXION)
ORDEN_BD.CommandType = CommandType.Text
ORDEN_BD.ExecuteNonQuery()
SQL = ""
MsgBox("Registro
actualizado!")
lCONEXION.Close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ms1 As New System.IO.MemoryStream() 'Creamos el
MemoryStream.
PictureBox1.BackgroundImage.Save(ms1,
System.Drawing.Imaging.ImageFormat.Jpeg) 'Salvamos el imagen que tenomos cargada en el PictureBox en el
MemoryStream.
SQL = "UPDATE
CONFIGURACION SET LOGO = (@LOGO) WHERE ID=1" 'Creamos el Query.
CONEXION.Close()
CONEXION.Open() 'Abrimos la Coenxion
de la base de datos.
Dim cmd As New SqlCommand(SQL, CONEXION) 'Creamos el comando.
cmd.Parameters.Add(New SqlParameter("@LOGO", ms1.GetBuffer())) 'Agregamos el
MemoryStream a los parametros para poderlos guardar en la base de datos.
cmd.ExecuteNonQuery() 'ejecutamos el query.
CONEXION.Close()
cmd = Nothing
ms1.Dispose()
ms1.Close()
CONEXION.Close()
'MsgBox("La imagen ha sido
registrada", MsgBoxStyle.Information)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Catch ex As Exception
'MessageBox.Show(ex.Message)
CONEXION.Close()
lCONEXION.Close()
End Try
Call CONSULTA()
End Sub
Sub CONSULTA()
Try
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
lCONEXION.Open()
SQL = "SELECT * FROM
CONFIGURACION WHERE ID='1'"
ORDEN_BD = New OleDb.OleDbCommand(SQL, lCONEXION)
ORDEN_BD.CommandType = CommandType.Text
ORDEN_BD.ExecuteNonQuery()
REM Cierro la CONEXION
D_ADAPTER = New OleDbDataAdapter(ORDEN_BD)
D_TABLE = New DataTable
D_ADAPTER.Fill(D_TABLE)
DataGridView1.DataSource = D_TABLE
If D_TABLE.Rows.Count >= 1 Then
TXT_TITULO_PDF1.Text =
D_TABLE.Rows(0)("TITULO_PDF")
TXT_TITULO_PDF2.Text =
D_TABLE.Rows(0)("TITULO_PDF2")
TXT_DELIMITADOR.Text =
D_TABLE.Rows(0)("DELIMITADOR_TXT")
TXT_DIRECCION_PDF.Text =
D_TABLE.Rows(0)("RUTA_PDF")
TXT_DIRECCION_WORD.Text =
D_TABLE.Rows(0)("RUTA_WORD")
TXT_DIRECCION.Text =
D_TABLE.Rows(0)("RUTA_TXT")
Else
End If
SQL = ""
lCONEXION.Close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim dt As New DataTable
SQL = "SELECT * FROM
CONFIGURACION WHERE ID = 1"
da = New SqlDataAdapter(SQL, CONEXION)
CONEXION.Open()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim ms As New System.IO.MemoryStream() 'Creamos el
MemoryStream y poder cargar la imagen.
Dim imageBuffer() As Byte = CType(dt.Rows(0).Item("LOGO"), Byte()) 'Conbertimos la imagen
cargada en el datatable a Bytes.
ms = New System.IO.MemoryStream(imageBuffer) 'Cargamos el
MemoryStream con la imagen ya convertida en Bytes.
PictureBox1.BackgroundImage = Nothing 'Si existe una imagen
cargada en el PictureBox la borramos.
PictureBox1.BackgroundImage = (Image.FromStream(ms)) 'Cargamos la imagen al
PictureBox, Nota: Lo hacemos como .BackgroundImage pero igual lo podemos hacer
como .Image.
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch 'Ajustamos la imagen
al todo el PictureBox.
CONEXION.Close()
dt.Clear()
dt.Reset()
ms.Dispose()
ms.Close()
SQL = ""
End If
CONEXION.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call CONSULTA()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim openFileDialog1 As New OpenFileDialog()
Dim dialogo As New DialogResult
openFileDialog1.Filter = "Imagen
(JPG,BMP,PNG)|*.JPG;*.BMP;*.PNG|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
dialogo =
openFileDialog1.ShowDialog()
If (dialogo = DialogResult.OK) Then
Dim bmp As New Bitmap(Image.FromFile(openFileDialog1.FileName))
bmp =
bmp.GetThumbnailImage(480, 480, Nothing, IntPtr.Zero) 'Redimencionamos pixeles deimagen (Opcional).
PictureBox1.BackgroundImage = Nothing
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch 'Ajustamos la imagen a todo el
picturebox.
PictureBox1.BackgroundImage =
bmp 'Cargamos
la imagen al PictureBox.
'sExtencion =
System.IO.Path.GetExtension(openFileDialog1.FileName) 'Obtenemos la Extencion
de la imagen cargada.
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End ClassResultado:




Excelente explicación, simple y funciona perfectamente
ResponderEliminar