Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

marcosbruno.mb

Pegar imagem bytearray e transformar em imagem

Recommended Posts

bom dia

 

preciso de uma ajuda

 

tenho uma imagem em bytearray no banco de dados, e preciso que minha página asp leia esse código todo e transforme em uma imagem

 

é possível fazer isso em asp?

 

o primeiro problema é que o asp não está lendo esse código gigante, ele mostra ?? em vez do código

 

e o segundo é depois que eu conseguir fazer o asp pegar esse código, preciso transformar isso em imagem novamente

 

alguém tem uma luz? obrigado

Compartilhar este post


Link para o post
Compartilhar em outros sites

Sim, é possível, Como você lida com os diferentes pedaços binários enquanto manipulá-os, e para armazená-los antes de colocar os pedaços juntos novamente, você pode usar a classe de VBScript que torna essa tarefa um pouco mais fácil. Ele converte os dados binários para cadeias hexadecimais que são anexados a um arquivo de texto. Em seguida, esse arquivo é carregado e as strings hexagonais são convertidos novamente em um array de bytes utilizando o objeto ADO Stream.

 

Primeiro de tudo use o include para a classe.

<!--#include file="cByteArray.asp"-->

Para chamar a classe cByteArray:

Dim sFilePath, oByte, ByteArray, lngBytes

'// Load a binary file, in this case an image.
sFilePath = Server.MapPath("SomeBinaryFile.gif")

Set oByte = New cByteArray

With oByte
'// Add some binary data as a byte array
Call .AddBytes(LoadBytes(sFilePath))
'// You may add as many binary chunks as you like.
'// If you are using the MidB function the binary data is transferred as a string variable.
'// This works just as well and gives us a handy tool to edit binary files.
Call .AddBytes(MidB(LoadBytes(sFilePath), 50, 100))
'// Read the number of bytes transferred to the class.
lngBytes = .BytesTotal
'// Return the bytes as a byte array. For example you may now save this
'// to a binary file using the ADO Stream object.
ByteArray = .ReturnBytes
End With

Set oByte = Nothing

Aqui a rotina loadBytes. Estamos usando o objeto ADO Stream para carregar um arquivo binário em um array de bytes...:

Function LoadBytes(sFile)
On Error Resume Next

Dim oStream

If IsEmpty(oStream) Then Set oStream = Server.CreateObject("ADODB.Stream")

With oStream
If .State = adStateOpen Then .State = adStateClosed
.Type = adTypeBinary
.Open
.LoadFromFile sFile
LoadBytes = .Read
End With

oStream.Close
Set oStream = Nothing

End Function

 

Só para completar, aqui está uma rotina que você pode usar para salvar o array de bytes retornado para um arquivo:

 

Sub SaveBytesToBinaryFile(ByteArray, sSavePath)
On Error Resume Next

Dim oStream

If LenB(ByteArray) = 0 Then Exit Sub

If IsEmpty(oStream) Then Set oStream = Server.CreateObject("ADODB.Stream")

With oStream
If .State = adStateOpen Then .State = adStateClosed
.Type = adTypeBinary
Call .Open
Call .Write(ByteArray)
Call .SaveToFile(sSavePath, adSaveCreateOverWrite)
Call .Close
End With

Set oStream = Nothing

End Sub

 

 

 

 

 

a classe:

<%

' ROUTINES:

' - Public Property Get BytesTotal()

' - Private Sub Class_Initialize()
' - Private Sub Class_Terminate()
' - Public Sub AddBytes(bytes)
' - Public Function ReturnBytes()
' - Private Function OctetToHexStr(arrbytOctet)
' - Private Sub ConvertHexStringToByteArray(ByVal strHexString, 

ByRef pByteArray)
' - Private Function ReadFile()
' - Private Sub DeleteTempFile()
'============================================================

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'============================================================
Class cByteArray
'============================================================


'// MODULE VARIABLES
Private m_lngByteSize                       '// Size of byte 

array
Private m_sTmpPath                          '// Temp file 

holding the hexed string(s)

'// MODULE PROPERTIES
Public Property Get BytesTotal()
    BytesTotal = m_lngByteSize
End Property

'---------------------------------------------------------------

---------------------------------------------
' Comment: Initialize our module variables
'---------------------------------------------------------------

---------------------------------------------
Private Sub Class_Initialize()
    On Error Resume Next

    m_lngByteSize = 0
    m_sTmpPath = Server.MapPath("temp.txt")

End Sub

'---------------------------------------------------------------

-----------------------------------------
' Comment: Clean up
'---------------------------------------------------------------

-----------------------------------------
Private Sub Class_Terminate()
    On Error Resume Next

    Call DeleteTempFile

End Sub

'---------------------------------------------------------------

---------------------------------------------
' Comment: Append string chuncks to a temp file.
'---------------------------------------------------------------

---------------------------------------------
Public Sub AddBytes(bytes)
    On Error Resume Next

    Dim oFSO, oFile, sHexString

    If LenB(bytes) = 0 Then Exit Sub

    '// Convert the string chunks to hexed characters and add 

them to a temp text file.
    sHexString = OctetToHexStr(bytes)

    '// Set the new number of bytes
    m_lngByteSize = m_lngByteSize + LenB(bytes)

    If IsEmpty(oFSO) Then Set oFSO = Server.CreateObject

("Scripting.FileSystemObject")
    Set oFile = oFSO.OpenTextFile(m_sTmpPath, ForAppending, 

True)
    oFile.Write sHexString
    oFile.Close
    
    Set oFile = Nothing
    Set oFSO = Nothing

End Sub

'---------------------------------------------------------------

---------------------------------------------
' Comment: Return all bytes.
'---------------------------------------------------------------

---------------------------------------------
Public Function ReturnBytes()
    On Error Resume Next

    Dim sHex, arrBytes

    '// Load the hexed characters from the temp text file ..
    sHex = ReadFile
    '// .. and convert them back into a byte array. We pass an 

empty variant byref
    '// that will be set as a byte array.
    Call ConvertHexStringToByteArray(sHex, arrBytes)

    ReturnBytes = arrBytes

End Function

'---------------------------------------------------------------

---------------------------------------------
' Comment: 

http://blogs.brnets.com/michael/archive/2005/03/09/387.aspx
'---------------------------------------------------------------

---------------------------------------------
Private Function OctetToHexStr(arrbytOctet)
    On Error Resume Next
    ' Function to convert OctetString (byte array) to Hex 

string.
    ' Code from Richard Mueller, a MS MVP in Scripting and ADSI

    Dim k

    OctetToHexStr = ""

    For k = 1 To LenB(arrbytOctet)
        OctetToHexStr = OctetToHexStr & Right("0" & Hex(AscB

(MidB(arrbytOctet, k, 1))), 2)
    Next

End Function

'---------------------------------------------------------------

---------------------------------------------
' Comment: 

http://blogs.brnets.com/michael/archive/2005/03/09/387.aspx
'---------------------------------------------------------------

---------------------------------------------
Private Sub ConvertHexStringToByteArray(ByVal strHexString, _
                                        ByRef pByteArray)
    On Error Resume Next

    Dim fso, stream, temp, ts, n
    ' This is an elegant way to convert a hex string to a Byte
    ' array. Typename(pByteArray) will return Byte(). pByteArray
    ' should be a null variant upon entry. strHexString should 

be
    ' an ASCII string containing nothing but hex characters, 

e.g.,
    ' FD70C1BC2206240B828F7AE31FEB55BE
    ' Code from Michael Harris, a MS MVP in Scripting

    Set fso = Server.CreateObject("scripting.filesystemobject")
    Set stream = Server.CreateObject("adodb.stream")

    temp = Server.MapPath(fso.gettempname)
    Set ts = fso.createtextfile(temp)

    For n = 1 To (Len(strHexString) - 1) Step 2
        ts.Write Chr("&h" & Mid(strHexString, n, 2))
    Next

    ts.Close

    stream.Type = 1
    stream.Open
    stream.LoadFromFile temp
    pByteArray = stream.Read

    stream.Close
    fso.DeleteFile temp

    Set stream = Nothing
    Set fso = Nothing

End Sub

'---------------------------------------------------------------

---------------------------------------------
' Comment: Read in a text file.
'---------------------------------------------------------------

---------------------------------------------
Private Function ReadFile()
    On Error Resume Next

    Dim oFSO
    Dim oFile
 
    If IsEmpty(oFSO) Then Set oFSO = Server.CreateObject

("Scripting.FileSystemObject")
    If Not oFSO.FileExists(m_sTmpPath) Then Exit Function
    Set oFile = oFSO.OpenTextFile(m_sTmpPath, ForReading, False)
    
    ReadFile = oFile.ReadAll

    Set oFile = Nothing
    Set oFSO = Nothing

End Function

'---------------------------------------------------------------

---------------------------------------------
' Comment: Delete temporary file.
'---------------------------------------------------------------

---------------------------------------------
Private Sub DeleteTempFile()
    On Error Resume Next
    
    Dim oFSO

    If IsEmpty(oFSO) Then Set oFSO = Server.CreateObject

("Scripting.FileSystemObject")
    If oFSO.FileExists(m_sTmpPath) Then oFSO.DeleteFile 

(m_sTmpPath)

    Set oFSO = Nothing

End Sub

'============================================================
End Class
'============================================================
%>

Compartilhar este post


Link para o post
Compartilhar em outros sites

chame através do caminho da mesma

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.