base64 enc/dec problem

P

PCH

I'm having a problem encoding a file (image) into base64, and then
converting it back from base64 and saving it.

I've tried several ways, but whenever i open the new image.. it is always
corrupt.

Here is the base coding I'm trying to get working

Imports System.IO

Dim fs As FileStream

Dim fswrite As FileStream

Dim oByte() As Byte

fs = New System.IO.FileStream("C:\18.jpg", _

System.IO.FileMode.Open, _

System.IO.FileAccess.Read)



ReDim oByte(fs.Length)

Dim b64String As String



b64String = System.Convert.ToBase64String(oByte, 0, fs.Length)

fswrite = File.Create("C:\test1.jpg", 1024)

Dim info As Byte() = New UTF8Encoding(True).GetBytes(b64String)

fswrite.Write(info, 0, info.Length)

fswrite.Close()

fs.Close()
 
D

David Browne

PCH said:
I'm having a problem encoding a file (image) into base64, and then
converting it back from base64 and saving it.

I've tried several ways, but whenever i open the new image.. it is always
corrupt.
First you were never reading the input file. Second you were saving the
base64 encoded string to the output file. Here:

Dim fs As FileStream
Dim fswrite As FileStream
Dim oByte() As Byte
fs = New System.IO.FileStream("C:\18.jpg", _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read)
ReDim oByte(fs.Length)
fs.Read(oByte, 0, fs.Length)
Dim b64String As String
b64String = System.Convert.ToBase64String(oByte, 0, fs.Length)
fswrite = File.Create("C:\test1.jpg", 1024)
Dim info As Byte() = System.Convert.FromBase64String(b64String)
fswrite.Write(info, 0, info.Length)
fswrite.Close()
fs.Close()

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top