30/06/2012, 14:52
|
| | | Fecha de Ingreso: marzo-2005 Ubicación: trujillo
Mensajes: 141
Antigüedad: 19 años, 9 meses Puntos: 0 | |
Respuesta: PictureBox enlazado a Combobox aca generosamente te dejo un código de manejo de imágenes:
Código:
Imports System.IO
Imports SYSTEM.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
#Region "VARIABLES"
#Region "Windows"
Private Declare Function ShellEx Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Integer, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
#End Region
#End Region
#Region "GUARDAR ARCHIVO"
Public Function obtieneCadenaArchivo(ByVal Archivo As String) As String
If LTrim(RTrim(Archivo)) = "" Then
Return ""
End If
Dim sFileToUpload As String = ""
sFileToUpload = LTrim(RTrim(Archivo))
Dim Extension As String = System.IO.Path.GetExtension(sFileToUpload)
If Extension = ".exe" Then
Return ""
End If
Return sFileToUpload
End Function
Public Function obtieneArchivoEnBinario(ByVal sPath As String) As Byte()
Dim data As Byte() = Nothing
Dim fInfo As New FileInfo(sPath)
Dim numBytes As Long = fInfo.Length
Dim fStream As New FileStream(sPath, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fStream)
data = br.ReadBytes(CInt(numBytes))
Return data
End Function
#End Region
#Region "MOSTRAR ARCHIVO"
Public Sub leerArchivoBinario(ByVal sFileName As String, ByVal sFileExtension As String, ByVal frm As Form, ByVal cnn As SqlClient.SqlConnection, ByVal Binario As Byte(), ByVal starTupath As String, ByVal strCarpeta As String, Optional ByVal abrir As Boolean = True, Optional ByVal ruta As String = "")
If ruta = "" Then
ruta = starTupath
End If
Dim Carpeta As String = strCarpeta
Try
Dim fileData As Byte() = Binario 'DirectCast(sqlCmd.ExecuteScalar(), Byte())
Dim sTempFileName As String = ruta & "\" + Carpeta + "\" & sFileName
If IO.File.Exists(ruta & "\" + Carpeta + "\" + sFileName) Then
IO.File.Delete(ruta & "\" + Carpeta + "\" + sFileName)
End If
If Not fileData Is Nothing Then
' Read image data into a file stream
Using fs As New FileStream(ruta & "\" + Carpeta + "\" + sFileName, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(fileData, 0, fileData.Length)
' Set image variable value using memory stream.
fs.Flush()
fs.Close()
End Using
' Open File ' 10 = SW_SHOWDEFAULT
If abrir Then
ShellEx(CInt(frm.Handle), "Open", ruta & "\" + Carpeta + "\" + sFileName, "", "", 10)
End If
End If
Catch ex As Exception
MessageBox.Show("EL ARCHIVO YA ESTA EN USO", "BANCO DE ARCHIVOS", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
#End Region
Public Sub abrirArchivo(ByVal frm As Form, ByVal ruta As String)
ShellEx(CInt(frm.Handle), "Open", ruta, "", "", 10)
End Sub
#Region "BUSCAR ARCHIVO"
Public Function buscarArchivo(ByVal strExtensiones As String) As String
Dim openFileDialog As New OpenFileDialog
openFileDialog.CheckPathExists = True
openFileDialog.CheckFileExists = True
openFileDialog.Filter = "Archivos (*." + strExtensiones + ")|*." + strExtensiones
openFileDialog.Multiselect = False
openFileDialog.AddExtension = True
openFileDialog.ValidateNames = True
Using OpenFileDialog1 As OpenFileDialog = openFileDialog
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Return OpenFileDialog1.FileName
End If
End Using
Return ""
End Function
#End Region
Public Function convertirBytes2Image(ByVal bytes() As Byte) As Image
If bytes Is Nothing Then Return Nothing
'
Dim ms As New MemoryStream(bytes)
Dim bm As Bitmap = Nothing
Try
bm = New Bitmap(ms)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
End Try
Return bm
End Function
Public Function convertirImage2Bytes(ByVal img As Image) As Byte()
Dim sTemp As String = Path.GetTempFileName()
Dim fs As New FileStream(sTemp, FileMode.OpenOrCreate, FileAccess.ReadWrite)
img.Save(fs, System.Drawing.Imaging.ImageFormat.Png)
fs.Position = 0
'
Dim imgLength As Integer = CInt(fs.Length)
Dim bytes(0 To imgLength - 1) As Byte
fs.Read(bytes, 0, imgLength)
fs.Close()
Return bytes
End Function
|