base64 enc/dec problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PCH

    base64 enc/dec problem

    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.FileS tream("C:\18.jp g", _

    System.IO.FileM ode.Open, _

    System.IO.FileA ccess.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(Tr ue).GetBytes(b6 4String)

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

    fswrite.Close()

    fs.Close()


  • David Browne

    #2
    Re: base64 enc/dec problem


    "PCH" <pch12@hotmail. com> wrote in message
    news:Omaoeb$PDH A.704@tk2msftng p13.phx.gbl...[color=blue]
    > 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.
    >[/color]
    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.FileS tream("C:\18.jp g", _
    System.IO.FileM ode.Open, _
    System.IO.FileA ccess.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. FromBase64Strin g(b64String)
    fswrite.Write(i nfo, 0, info.Length)
    fswrite.Close()
    fs.Close()

    David


    Comment

    • PCH

      #3
      Re: base64 enc/dec problem

      ah that Read call!
      fs.Read(oByte, 0, fs.Length)

      Thanks for the info!



      "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
      message news:%23wydej$P DHA.3880@tk2msf tngp13.phx.gbl. ..[color=blue]
      >
      > "PCH" <pch12@hotmail. com> wrote in message
      > news:Omaoeb$PDH A.704@tk2msftng p13.phx.gbl...[color=green]
      > > 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[/color][/color]
      always[color=blue][color=green]
      > > corrupt.
      > >[/color]
      > 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.FileS tream("C:\18.jp g", _
      > System.IO.FileM ode.Open, _
      > System.IO.FileA ccess.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. FromBase64Strin g(b64String)
      > fswrite.Write(i nfo, 0, info.Length)
      > fswrite.Close()
      > fs.Close()
      >
      > David
      >
      >[/color]


      Comment

      Working...