PRECISO DE AJUDA COM VARI?VEL TIPO BYTE

ELMO01 27/01/2017 10:33:38
#471090
Oi pessoal,

Estou com um problema, tenho uma variável do MYSQL tipo LONGBLOB onde guardo imagens, o problema não é salvar esta imagem mais sim de apresentar esta imagem em um PICTUREBOX. Quando verifico se o campo que guardo esta imagem está vazia me retorna o seguinte erro:

NÃO é POSSÍVEL CONVERTER UM OBJETO DO TIPO [Ô]SYSTEM.DBNULL[Ô] NO TIPO SYSTEM.BYTE[]

Abaixo está parte do código onde faço a apresentação da figura ou foto que está salva no banco de dados.

If Not IsDBNull(dr_Empresa.Item([Ô]Foto[Ô])) Then
Dim byteImage() As Byte = dr_Empresa.Item([Ô]Foto[Ô])
Me.Pxb_Foto2.Image = Nothing
Dim stmFoto As New System.IO.MemoryStream(byteImage)
Pxb_Foto2.Image = Image.FromStream(stmFoto)
Else
Me.Pxb_Foto2.Image = Image.FromFile(PathFoto & [Ô]Security_Reader1.png[Ô])
End If

Por favor preciso de ajuda para saber como faço para apresentar a foto ou imagem gravada no banco de dados.

PS:
No banco de dados a variável é:

Nome: Foto
tipo: LongBlob

Obrigado.
TUNUSAT 27/01/2017 11:03:09
#471091
ELMO01,

Achei algo interessante. Talvez sirva para você:

----------------------------------------------------------

Salvar e recuperar imagens BLOB do banco de dados MySQL em ASP.Net, C # e VB.Net

http://www.aspsnippets.com/Articles/Save-and-Retrieve-BLOB-Images-from-MySql-Database-in-ASPNet-C-and-VBNet.aspx

Exibindo as imagens binárias (BLOB) no controle de imagem no GridView

O seguinte manipulador de eventos OnRowDataBound faz o trabalho de exibir os dados de imagem binária (BLOB) no controle de imagem.

Primeiro, os dados BLOB são obtidos da propriedade GridView DataItem e são convertidos de volta para a matriz BYTE, que é então convertida em uma seqüência BASE64 e atribuída à propriedade ImageUrl do controle Image.

----------------------------------------------------------

  Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

Dim bytes As Byte() = TryCast(TryCast(e.Row.DataItem, DataRowView)([Ô]Content[Ô]), Byte())

Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)

TryCast(e.Row.FindControl([Ô]Image1[Ô]), Image).ImageUrl = Convert.ToString([Ô]data:image/png;base64,[Ô]) & base64String

End If

End Sub


[][ô]s,
Tunusat.
DAMASCENO.CESAR 27/01/2017 11:29:35
#471092
Resposta escolhida
ELMO01, uso isso aqui:
  #Region [Ô]Using Statements[Ô]

Imports System
Imports System.Collections.Generic
Imports System.Text

Imports System.IO
Imports System.Data
Imports MySql.Data.MySqlClient
#End Region

Namespace ImgDataTable

Partial Public Class CategoriesTableAdapter
Private Connection As String = [Ô]sua conexao[Ô]
[ô][ô][ô] <summary>
[ô][ô][ô] The insertNewImage method takes all the information about the image and stores it
[ô][ô][ô] in the database. This method accesses a stored procedure to insert the data and returns
[ô][ô][ô] the success statement or error message.
[ô][ô][ô] </summary>
[ô][ô][ô] <param name=[Ô]CategoryID[Ô]></param>
[ô][ô][ô] <param name=[Ô]photographName[Ô]></param>
[ô][ô][ô] <param name=[Ô]myBuffer[Ô]></param>
[ô][ô][ô] <returns></returns>
[ô][ô][ô]
Public Function insertNewImage(ByVal CategoryID As Integer, ByVal photographName As String, ByRef myBuffer() As Byte) As String
Return [Ô][Ô]
End Function

Public Function insertNImage(ByVal StrSQL As String, ByRef myBuffer() As Byte) As String

Dim message As String = [Ô][Ô]
Dim myConnection As MySqlConnection

myConnection = New MySqlConnection(Connection)

Try
myConnection.Open()

[ô][ô] Create a stored procedure command
Dim myCommand As New MySqlCommand
[ô]
myCommand = myConnection.CreateCommand
myCommand.CommandText = StrSQL

[ô][ô] Add the image parameter and set myBuffer as the value.
myCommand.Parameters.Add([Ô]@image[Ô], MySqlDbType.Blob).Value = myBuffer

[ô][ô] Execute the insert
myCommand.ExecuteNonQuery()

[ô][ô] Close the Connection
myConnection.Close()

[ô][ô] Assign the success message
message = [Ô]foto inserida![Ô]

Catch ex As Exception
MsgBox(Err.Number & [Ô] - [Ô] & ex.Message)
[ô][ô] Assign the error message
message = Err.Number & [Ô] - [Ô] & ex.Message
End Try
Return message

End Function

[ô][ô][ô] <summary>
[ô][ô][ô] The getCategories method is a general method that returns a DataSet with Category
[ô][ô][ô] information in it.
[ô][ô][ô] </summary>
[ô][ô][ô] <returns></returns>
Public Function getCategories(ByVal MyQuery As String) As DataSet

Dim myConnection As MySqlConnection
Dim myCommand As MySqlCommand

Dim myDataSet As DataSet = New DataSet()
Dim myAdapter As MySqlDataAdapter
myConnection = New MySqlConnection(Connection)
Try
myConnection.Open()
myCommand = New MySqlCommand()
myCommand.CommandText = MyQuery
myCommand.Connection = myConnection
myAdapter = New MySqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
myConnection.Close()

Catch ex As Exception
Throw ex
End Try

Return myDataSet
End Function

[ô][ô][ô] <summary>
[ô][ô][ô] The getImages method accesses the database and return Image information
[ô][ô][ô] based on the CategoryID that is passed in.
[ô][ô][ô] </summary>
[ô][ô][ô] param name=[Ô]CategoryID[Ô]/param
[ô][ô][ô] <returns></returns>
[ô][ô][ô]
Public Function getImages(ByVal MyQuery As String) As DataSet
Dim myConnection As MySqlConnection
Dim myCommand As MySqlCommand
Dim myDataSet As DataSet = New DataSet()
Dim myAdapter As MySqlDataAdapter
myConnection = New MySqlConnection(Connection)
Try
myConnection.Open()
myCommand = New MySqlCommand()
myCommand.CommandText = MyQuery
myCommand.Connection = myConnection
myAdapter = New MySqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
Catch ex As Exception

Throw ex
End Try

Return myDataSet
End Function


End Class
End Namespace


e voce coloca isso no form

  
Private Sub CarregaImg()
Try
[ô][ô] create a new adapter object
Dim myAdapter As CategoriesTableAdapter = New CategoriesTableAdapter()

[ô][ô] DataSet of image information
myPhotoDataSet = myAdapter.getImages(Str)

If myPhotoDataSet.Tables(0).Rows.Count > 0 Then
[ô][ô] Get the image data and stored it in a byte array
Dim myByteArray() As Byte
myByteArray = IIf(IsDBNull(myPhotoDataSet.Tables(0).Rows(currentPage).Item([Ô]Foton[Ô])), Nothing, myPhotoDataSet.Tables(0).Rows(currentPage).Item([Ô]Foton[Ô]))
If Not IsNothing(myByteArray) Then
[ô][ô] Create a new MemoryStream and write all the information from
[ô][ô] the byte array into the stream
Dim myStream As MemoryStream = New MemoryStream(myByteArray, True)
myStream.Write(myByteArray, 0, myByteArray.Length)

[ô][ô] Use the MemoryStream to create the new BitMap object
Dim FinalImage As Bitmap = New Bitmap(myStream)

[ô][ô] See if the image stored will fit in the picture box. If it[ô]s too big,
[ô][ô] resize and create a new BitMap object and assign to the PictureBox control
If FinalImage.Width > 120 And FinalImage.Height > 160 Then
[ô]Dim AlteredImage As Bitmap = New Bitmap(FinalImage, New Size(PctFoto.Width, PctFoto.Height))
[ô]PctFoto.Image = AlteredImage
PctFoto.Image = FinalImage
Else
PctFoto.Image = FinalImage
End If
[ô][ô] Close the stream
myStream.Close()
Else
Fft = False
End If
Else
MessageBox.Show([Ô]Não imagens para mostrar[Ô])
Fft = False
End If
Catch ex As Exception
[ô]MessageBox.Show([Ô]There was an error displaying this image.[Ô], [Ô]Alert[Ô])
MsgBox(Err.Number & [Ô] - [Ô] & ex.Message)
End Try
End Sub
PLUGSOFTSM 27/01/2017 12:20:23
#471096
Tenta fazer assim

If Not IsDBNull(dr_Empresa.Item([Ô]Foto[Ô])) Then
Dim Img As New MemoryStream(CType( dr_Empresa.Item([Ô]Foto[Ô]), Byte()))
Me.Pxb_Foto2.Image = Image.FromStream(Img)
Else
Me.Pxb_Foto2.Image = Nothing
End If


ELMO01 27/01/2017 12:21:16
#471097
Pessoal, muito obrigado por ter respondido, deu certo.
Tópico encerrado , respostas não são mais permitidas