Solved

Base64 Encoding in .NET

Posted on 2004-10-24
532 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
Question by:odonovanpm
    3 Comments
     
    LVL 48

    Expert Comment

    by:Ryan Chong
    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
    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:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now