Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
VB.NET
Graphics/GDI+
Convert base 64
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Gawron" data-source="post: 23438" data-attributes="member: 5648"><p>I have an application that goes out to a shipping company and gets the shipping label for the box being shipped. I am able to generate the XML and get the label from the company. I can display the label in a text box as a string. I need to be able to convert this label to a GIF file so I can redirect the user to this file. Below is the code I tried to use. It creates a file, but not one that can be displayed. Thanks for the help.</p><p></p><p><span style="font-size: 10px"><span style="color: #0000ff">Public</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Sub</span></span><span style="font-size: 10px"> ConvertFiletoBinary()</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> inFile </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.IO.StreamReader</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> base64String </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span> <span style="font-size: 10px"><span style="color: #0000ff">String</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> inputFilename </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span> <span style="font-size: 10px"><span style="color: #0000ff">String</span></span><span style="font-size: 10px"> = "c:\j.txt"</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> outputfilename </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span> <span style="font-size: 10px"><span style="color: #0000ff">String</span></span><span style="font-size: 10px"> = "C:\nike.gif"</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> base64CharArray() </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Char</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px">inFile = </span><span style="font-size: 10px"><span style="color: #0000ff">New</span></span><span style="font-size: 10px"> System.IO.StreamReader(inputFileName, _</span></p><p><span style="font-size: 10px">System.Text.Encoding.ASCII)</span></p><p><span style="font-size: 10px">base64CharArray = </span><span style="font-size: 10px"><span style="color: #0000ff">New</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Char</span></span><span style="font-size: 10px">(inFile.BaseStream.Length) {}</span></p><p><span style="font-size: 10px">inFile.Read(base64CharArray, 0, inFile.BaseStream.Length)</span></p><p><span style="font-size: 10px">base64String = </span><span style="font-size: 10px"><span style="color: #0000ff">New</span></span> <span style="font-size: 10px"><span style="color: #0000ff">String</span></span><span style="font-size: 10px">(base64CharArray, _</span></p><p><span style="font-size: 10px">0, _</span></p><p><span style="font-size: 10px">base64CharArray.Length - 1)</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Catch</span></span><span style="font-size: 10px"> exp </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.Exception</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #008000">' Error creating stream or reading from it.</span></span></p><p><span style="font-size: 10px"><span style="color: #008000"></span></span><span style="font-size: 10px">System.Console.WriteLine("{0}", exp.Message)</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Return</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">End</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #008000">' Convert the Base64 UUEncoded input into binary output.</span></span></p><p><span style="font-size: 10px"><span style="color: #008000"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> binaryData() </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Byte</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px">binaryData = System.Convert.FromBase64String(base64String)</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Catch</span></span><span style="font-size: 10px"> exp </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.ArgumentNullException</span></p><p><span style="font-size: 10px">System.Console.WriteLine("Base 64 string is null.")</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Return</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Catch</span></span><span style="font-size: 10px"> exp </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.FormatException</span></p><p><span style="font-size: 10px">System.Console.WriteLine("Base 64 length is not 4 or is " + _</span></p><p><span style="font-size: 10px">"not an even multiple of 4.")</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Return</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">End</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #008000">'Write out the decoded data.</span></span></p><p><span style="font-size: 10px"><span style="color: #008000"></span></span><span style="font-size: 10px"><span style="color: #0000ff">Dim</span></span><span style="font-size: 10px"> outFile </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.IO.FileStream</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px">outFile = </span><span style="font-size: 10px"><span style="color: #0000ff">New</span></span><span style="font-size: 10px"> System.IO.FileStream(outputFileName, _</span></p><p><span style="font-size: 10px">System.IO.FileMode.Create, _</span></p><p><span style="font-size: 10px">System.IO.FileAccess.Write)</span></p><p><span style="font-size: 10px">outFile.Write(binaryData, 0, binaryData.Length - 1)</span></p><p><span style="font-size: 10px">outFile.Close()</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">Catch</span></span><span style="font-size: 10px"> exp </span><span style="font-size: 10px"><span style="color: #0000ff">As</span></span><span style="font-size: 10px"> System.Exception</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #008000">' Error creating stream or writing to it.</span></span></p><p><span style="font-size: 10px"><span style="color: #008000"></span></span><span style="font-size: 10px">System.Console.WriteLine("{0}", exp.Message)</span></p><p><span style="font-size: 10px"></span><span style="font-size: 10px"><span style="color: #0000ff">End</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Try</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span><span style="font-size: 10px"><span style="color: #0000ff">End</span></span> <span style="font-size: 10px"><span style="color: #0000ff">Sub</span></span></p><p><span style="font-size: 10px"><span style="color: #0000ff"></span></span></p></blockquote><p></p>
[QUOTE="Gawron, post: 23438, member: 5648"] I have an application that goes out to a shipping company and gets the shipping label for the box being shipped. I am able to generate the XML and get the label from the company. I can display the label in a text box as a string. I need to be able to convert this label to a GIF file so I can redirect the user to this file. Below is the code I tried to use. It creates a file, but not one that can be displayed. Thanks for the help. [SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ConvertFiletoBinary() [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] inFile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.StreamReader [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] base64String [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] inputFilename [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "c:\j.txt" [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] outputfilename [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "C:\nike.gif" [/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] base64CharArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Char [/COLOR][/SIZE][SIZE=2]inFile = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.StreamReader(inputFileName, _ System.Text.Encoding.ASCII) base64CharArray = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Char[/COLOR][/SIZE][SIZE=2](inFile.BaseStream.Length) {} inFile.Read(base64CharArray, 0, inFile.BaseStream.Length) base64String = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2](base64CharArray, _ 0, _ base64CharArray.Length - 1) [/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Exception [/SIZE][SIZE=2][COLOR=#008000]' Error creating stream or reading from it. [/COLOR][/SIZE][SIZE=2]System.Console.WriteLine("{0}", exp.Message) [/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Convert the Base64 UUEncoded input into binary output. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] binaryData() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Byte [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2]binaryData = System.Convert.FromBase64String(base64String) [/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ArgumentNullException System.Console.WriteLine("Base 64 string is null.") [/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.FormatException System.Console.WriteLine("Base 64 length is not 4 or is " + _ "not an even multiple of 4.") [/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Write out the decoded data. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] outFile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.FileStream [/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2]outFile = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.FileStream(outputFileName, _ System.IO.FileMode.Create, _ System.IO.FileAccess.Write) outFile.Write(binaryData, 0, binaryData.Length - 1) outFile.Close() [/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Exception [/SIZE][SIZE=2][COLOR=#008000]' Error creating stream or writing to it. [/COLOR][/SIZE][SIZE=2]System.Console.WriteLine("{0}", exp.Message) [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE] [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
VB.NET
Graphics/GDI+
Convert base 64
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom