base64 enc/dec problem

  • Thread starter Thread starter PCH
  • Start date Start date
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()
 
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
 
Back
Top