Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Base64 Encoding in .NET

Posted on 2004-10-24
3
Medium Priority
?
537 Views
Last Modified: 2010-09-04
I am using VB.NET and need to encode a jpeg or gif image using base64, how do I go about doing this? I have an old VB6 class that can do this but I imagine there must be a new class available in the .NET Framework that will allow me to encode a binary file such as jpeg/gif? All help is appreciated.
0
Comment
Question by:odonovanpm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 12396770
Try:

public static string FileToBase64(string fileName)
{
     byte[] binaryData;
     System.IO.FileStream inputFileName = new System.IO.FileStream(fileName,
                    System.IO.FileMode.Open, System.IO.FileAccess.Read);
     binaryData = new Byte[inputFileName.Length];
     inputFileName.Close();
     return System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}

More info at: http:Q_21121091.html

Hope this helps
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12396987
Try this (make sure you add Imports System.IO)

Function FileToBase64(ByVal path As String) As String
    Dim fs As New FileStream(path, FileMode.Open)
    Dim br As New BinaryReader(fs)
    Dim bytes(fs.Length - 1) As Byte

    bytes = br.ReadBytes(fs.Length)

    br.Close()
    fs.Close()

    Return Convert.ToBase64String(bytes)
End Function
0
 
LVL 15

Accepted Solution

by:
Timbo87 earned 1000 total points
ID: 12396997
I just realized you don't need minus 1 in the bytes array.

Function FileToBase64(ByVal path As String) As String
    Dim fs As New FileStream(path, FileMode.Open)
    Dim br As New BinaryReader(fs)
    Dim bytes(fs.Length) As Byte

    bytes = br.ReadBytes(fs.Length)

    br.Close()
    fs.Close()

    Return Convert.ToBase64String(bytes)
End Function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question