.NET Framework - Base64 and .gif

Asked By Jsimpson
26-Oct-08 07:07 AM
I am working with encodeing and decodeing image strings to base64.

My.Types.ps1xml

[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))

[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))

Update-TypeData c:\scripts\My.Types.ps1xml

$pic = Get-Content "c:\scripts\test.bmp"
$pice = $pic.ToBase64String

This works great.  I can unencode it and save it as a file of a
different name and all is well. Where I run into problems, is if I do
the same with a gif file

$gif = Get-Content "c:\scripts\test.gif"
$gife = $pic.ToBase64String

$gife is empty.  Doing a GM on both says they are System.String and
have all the same methods and properties includeing ToBase64String and
FromBase64String, but it doesn't work.  Anyone know what I am
missing?  The eventual goal is to be able to take a base64 encoded gif
strin from a XML response and save it as a gif file.

Thanks,
JSimpson
UNICODE.GetBytes
(1)
System.IO.MemoryStream
(1)
LoadWithPartialName
(1)
System.Convert
(1)
Drawing.Image
(1)
ReadAllBytes
(1)
FromStream
(1)
TypeData
(1)
  Jsimpson replied...
26-Oct-08 07:07 AM
($=ADthis))
in=ADg($this))

So I decided to cast the get-content variable as a sting.

[Sting]$gif =3D Get-Content "c:\scripts\test.gif"
$gife =3D $pic.ToBase64String

That seemed to work, but when I send it back to a file

$gife.FromBase64String | Set-Content "c:\scripts\LOGO_S2.gif"

It seams to work, but the image comes out scrambled.  It must have
something to do with the internal formating of a gif, but I am not
sure what....

JSimpson
  Kiro replied...
24-Oct-08 04:26 PM
Convert the image's bytes directly into a base64 string, then, to create the
new image file convert the base64 string back to bytes and pass them to
Set-Content's -Value parameter, be sure to use Byte encoding.
To get the byte array from the image file call [io.file]'s ReadAllBytes
method or use Get-Content and set its -Encoding parameter to Byte,
[io.file]'s ReadAllBytes method is much faster.

$byteArr = [io.file]::readAllBytes($file)
$byteArr = gc $file -en byte

$file = 'c:\scripts\test.gif'                                # fullpath
$encStr = [convert]::toBase64String([io.file]::readAllBytes($file))
sc c:\test.gif ([convert]::fromBase64String($encStr)) -en byte
ii c:\test.gif
sleep -m 250
ri c:\test.gif

--
Kiron
  Oisin (x0n) Grehan [MVP] replied...
01-Nov-08 06:57 AM
($=ADthis))
in=ADg($this))

Just a comment on the naming: I would expect ToBase64String to be a
(script)method because of the commonly used prefix "To." If you're
intent on using a (script)property, maybe rename it to

- Oisin
  Jsimpson replied...
01-Nov-08 06:57 AM
On Oct 26, 2:21=A0pm, "Oisin (x0n) Grehan [MVP]" <ois...@gmail.com>
es($=AD=ADthis))
trin=AD=ADg($this))
f

I actually gave up and did it with .net like this...

[void][reflection.assembly]::LoadWithPartialName("System.IO")
[Reflection.Assembly]::LoadWithPartialName('System.Drawing')

$pic =3D $xml.image.gif

$byte =3D [System.Convert]::FromBase64String("$pic")

$memoryStream =3D new-object System.IO.MemoryStream
$memoryStream.write($byte,0,$byte.Length)

$img=3D[Drawing.Image]::FromStream($memoryStream, 'Gif')

$img.Save("c:\scripts\pic.gif", 'Gif')

$img.Dispose()

I don't remember where I found it, but it works.  :)

Thanks for all the help.

JSimpson
Create New Account
help
buffer Dim ser As New System.Runtime.Serialization.Formatters.Binary. BinaryFormatter() Dim ms As New System.IO.MemoryStream() ser.Serialize(ms, obj) ms.Seek(0, IO.SeekOrigin.Begin) Dim buffer(ms.Length) As Byte ms.Read(buffer, 0, ms.Length - 1) bar = buffer EncodedObject = System.Convert.ToBase64String(buffer, Base64FormattingOptions.None) ms.Close() Return EncodedObject End Function Public Function Base64DecodeObject(ByVal EncodedObject Dim ser As New System.Runtime.Serialization.Formatters.Binary. BinaryFormatter Dim buffer As Byte() buffer = System.Convert.FromBase64String(EncodedObject) Dim ms As New System.IO.MemoryStream(buffer, True) Dim buffer2(buffer.Length - 1) As Byte Dim index As Integer
Public Function XMLSerializeMSToString(Of T)(ByVal Obj As Object) As String Dim MS As New System.IO.MemoryStream Dim XML As New System.Xml.Serialization.XmlSerializer(GetType(T)) XML.Serialize(MS, Obj) MS As New System.IO.StreamReader(MS) Dim Str As String = sr.ReadToEnd() sr.Close() Return System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(Str)) End Function 'funktioniert leider nicht Public Function XMLDeSerializeMSFromString(Of T)(ByVal DataObjectString As String) As Object Dim MS As New System.IO MemoryStream MS.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(System.Convert.ToString(DataObjectString)), 0, DataObjectString.Length) MS
mein StreamByte immer 0. Folgender Code wird verwendet: Dim Stream As System.IO.Stream = New System.IO.MemoryStream Dim xmlWriter As Xml.XmlWriter Dim xmlSettings As New Xml.XmlWriterSettings xmlSettings.Indent = True xmlWriter Kann mir jemand sagen, warum das so ist. . . - DANKE VB.NET - German Discussions AHAHAHAHAHAHAHAHAHAHAHAHAHAHJetzt (1) System.IO.MemoryStream (1) System.IO.Stream (1) WriteStartAttribute (1) WriteStartDocument (1) WriteStartElement (1) SeekOrigin.Begin (1) WriteEndDocument MemoryStream-Klasse schon, insofern ist also deine Anweisung Dim Stream As System.IO.Stream = New System.IO.MemoryStream etwas kontraproduktiv. - - AHAHAHAHAHAHAHAHAHAHAHAHAHAH Jetzt hat's der Bub auch verstanden. . . . Sorry, war auf der
DetalleOp / Numero que en nuestro caso seria un cero. Entonces hago esto: Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(StringXML)) Dim xpdoc As New XPathDocument(ms) Dim xpnd As cualquiera que me lea. salu2! ASP.NET - Spanish Discussions System.Text.Encoding.UTF8.GetBytes (1) System.IO.MemoryStream (1) XPathNodeIterator (1) XPathExpression (1) XPathDocument (1) MemoryStream (1) CreateNavigator (1) StringXML (1) keywords
which in this case would be 0. this is what i do: Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(StringXML)) Dim xpdoc As New XPathDocument(ms) Dim xpnd As with this code? thanks in advance! VB.NET Discussions System.Text.Encoding.UTF8.GetBytes (1) System.IO.MemoryStream (1) XPathNodeIterator (1) XPathExpression (1) XPathDocument (1) SelectSingleNode (1) MemoryStream (1) StringReader (1) You