Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Immagine to Byte
Forum - C# / VB.NET - Immagine to Byte

Avatar
blasters (Normal User)
Newbie


Messaggi: 8
Iscritto: 12/04/2010

Segnala al moderatore
Postato alle 18:53
Martedė, 06/03/2012
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

Codice sorgente - presumibilmente VB.NET

  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         TextBox1.Text = ConvertImageFiletoBytes("C:\ciao.jpg").ToString
  6.  
  7.     End Sub
  8.  
  9.  
  10.  
  11.  
  12.     Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
  13.         Dim _tempByte() As Byte = Nothing
  14.         If String.IsNullOrEmpty(ImageFilePath) = True Then
  15.             Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
  16.             Return Nothing
  17.         End If
  18.         Try
  19.             Dim _fileInfo As New IO.FileInfo(ImageFilePath)
  20.             Dim _NumBytes As Long = _fileInfo.Length
  21.             Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
  22.             Dim _BinaryReader As New IO.BinaryReader(_FStream)
  23.             _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
  24.             _fileInfo = Nothing
  25.             _NumBytes = 0
  26.             _FStream.Close()
  27.             _FStream.Dispose()
  28.             _BinaryReader.Close()
  29.             Return _tempByte
  30.         Catch ex As Exception
  31.             Return Nothing
  32.         End Try
  33.     End Function
  34. End Class




Dove sbaglio?

Grazie

PM
Avatar
mattia1481 (Member)
Pro


Messaggi: 84
Iscritto: 03/11/2008

Up
0
Down
V
Segnala al moderatore
Postato alle 8:11
Mercoledė, 07/03/2012
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.

PM
Avatar
Snogar (Normal User)
Pro


Messaggi: 145
Iscritto: 09/01/2012

Up
0
Down
V
Segnala al moderatore
Postato alle 18:17
Mercoledė, 07/03/2012
Forse vuoi fare una cosa di questo tipo?

Codice sorgente - presumibilmente VB.NET

  1. Imports System.IO
  2.      
  3.     Public Class Form1
  4.         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.             TextBox1.Text = ConvertImageFiletoBytes("C:\ciao.jpg")
  6.      
  7.         End Sub
  8.      
  9.     Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As String
  10.      
  11.       Dim _tempByte As String = ""
  12.  
  13.         If String.IsNullOrEmpty(ImageFilePath) = True Then
  14.             Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
  15.             Return Nothing
  16.         End If
  17.         Try
  18.             Dim _fileInfo As New IO.FileInfo(ImageFilePath)
  19.             Dim _NumBytes As Long = _fileInfo.Length
  20.             Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
  21.             Dim _BinaryReader As New IO.BinaryReader(_FStream)
  22.             For Indice = 0 To _NumBytes - 1
  23.                 _tempByte = _tempByte & " " & Conversion.Hex(Convert.ToString(_BinaryReader.ReadByte))
  24.             Next
  25.             _fileInfo = Nothing
  26.             _NumBytes = 0
  27.             _FStream.Close()
  28.             _FStream.Dispose()
  29.             _BinaryReader.Close()
  30.             Return _tempByte
  31.  
  32.         Catch ex As Exception
  33.             Return Nothing
  34.         End Try
  35.  
  36.     End Function


Ultima modifica effettuata da Snogar il 07/03/2012 alle 18:18
Ciao, grazie per la risposta.. Si vorrei una cosa simile, devo inviare una piccola immagine di qualche pixel ad un display lcd tramite arduino (il display č vt100, gestito da un bv4511) la cosa che mi sembra strana č che un immagine 10x10pixel possa diventare cosė grossa, mi escono tante righe - blasters - 07/03/12 18:59
PM