PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Converting Images to Base64 and Back Again-VBForums
Results 1 to 4 of 4

Thread: Converting Images to Base64 and Back Again

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Cool Converting Images to Base64 and Back Again

    Ever need to store a picture in a database? It's not the easiest thing to do, but this class I wrote makes it pretty quick and easy.

    The attached project consists of a VB.NET class that does all the hard work for you. With this class you can:

    1) Convert an image to a Base64 object
    2) Convert a Base64 object back to an image
    3) Convert an array of bytes to an image
    4) Convert an image to an array of bytes

    It works quickly and you will be able to save images as strings in your database. A string is a much easier thing to save than an image IMO.

    I also built out a quick sample project so you can see how the class works and how you can use it.

    Here's the code of my class:

    vb.net Code:
    1. Imports System.IO
    2. Imports System.Drawing
    3.  
    4. ''' <summary>
    5. ''' ImageToByteArray - Takes in an image and converts it to an array of bytes - PREFERRED METHOD
    6. ''' ImageToBase64    - Takes in an image and converts it to a Base64 string
    7. ''' ByteArrayToImage - Takes in a byte array and converts it to an image - PREFERRED METHOD
    8. ''' Base64ToImage    - Takes in a Base64 string and converts it to an image
    9. ''' </summary>
    10. ''' <remarks>Thanks to jmcilhinney for his insight and remarks on the following methods</remarks>
    11.  
    12. Public Class I2BB2I
    13.  
    14.     Public Shared Function ImageToByteArray(img As Image) As Byte()
    15.         Using strm As New MemoryStream
    16.             img.Save(strm, img.RawFormat)
    17.             Return strm.GetBuffer()
    18.         End Using
    19.     End Function
    20.  
    21.     Public Shared Function ImageToBase64(img As Image) As String
    22.         Return Convert.ToBase64String(ImageToByteArray(img))
    23.     End Function
    24.  
    25.     Public Overloads Shared Function ByteArrayToImage(data As Byte()) As Image
    26.         Using strm As New MemoryStream(data)
    27.             Return Image.FromStream(strm)
    28.         End Using
    29.     End Function
    30.  
    31.     Public Overloads Shared Function Base64ToImage(text As String) As Image
    32.         Return ByteArrayToImage(Convert.FromBase64String(text))
    33.     End Function
    34. End Class

    The attached example is a quick and dirty way of showing what the small class can do. This was written in VS2012, but should convert backwards pretty easily if you are looking to play with the source code. Feel free to modify it and do whatever you want to it.
    Attached Files Attached Files
    Last edited by mbutler755; Jan 16th, 2014 at 08:25 PM. Reason: Update class & project
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width