Oppure

Loading
06/03/12 17:53
blasters
Ciao a tutti,

vorrei convertire delle mie immagini in byte tipo questi 0xff,0xff,0xff,0xff,0xff per poi farli uscire in una textbox e utilizzarli in una seconda applicazione

ho trovato degli algoritmi ma come risultato ho sempre System.Byte[]

il codice da me utilizzato è il seguente

 Imports System.IO

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = ConvertImageFiletoBytes("C:\ciao.jpg").ToString

    End Sub




    Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
        Dim _tempByte() As Byte = Nothing
        If String.IsNullOrEmpty(ImageFilePath) = True Then
            Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
            Return Nothing
        End If
        Try
            Dim _fileInfo As New IO.FileInfo(ImageFilePath)
            Dim _NumBytes As Long = _fileInfo.Length
            Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim _BinaryReader As New IO.BinaryReader(_FStream)
            _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
            _fileInfo = Nothing
            _NumBytes = 0
            _FStream.Close()
            _FStream.Dispose()
            _BinaryReader.Close()
            Return _tempByte
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class 



Dove sbaglio?

Grazie
aaa
07/03/12 7:11
mattia1481
E' ovvio, fino a quando userai la funzione ToString della classe System.Array avrai sempre lo stesso risultato: la rappresentazione in stringa del type System.Array.

Per ottenere quello che desideri tu, devi implementare un ciclo che converte ogni item della matrice restituita dalla tua funzione ConvertImageFiletoBytes nel formato esadecimale, poi in stringa e conseguentemente accodare il risultato nella stringa "finale" che passerai alla tua TextBox.

Buon lavoro.
aaa
07/03/12 17:17
Snogar
Forse vuoi fare una cosa di questo tipo?

    Imports System.IO
     
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ConvertImageFiletoBytes("C:\ciao.jpg")
     
        End Sub
     
    Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As String
     
      Dim _tempByte As String = ""

        If String.IsNullOrEmpty(ImageFilePath) = True Then
            Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
            Return Nothing
        End If
        Try
            Dim _fileInfo As New IO.FileInfo(ImageFilePath)
            Dim _NumBytes As Long = _fileInfo.Length
            Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim _BinaryReader As New IO.BinaryReader(_FStream)
            For Indice = 0 To _NumBytes - 1
                _tempByte = _tempByte & " " & Conversion.Hex(Convert.ToString(_BinaryReader.ReadByte))
            Next
            _fileInfo = Nothing
            _NumBytes = 0
            _FStream.Close()
            _FStream.Dispose()
            _BinaryReader.Close()
            Return _tempByte

        Catch ex As Exception
            Return Nothing
        End Try

    End Function

Ultima modifica effettuata da Snogar 07/03/12 17:18
aaa