Results 1 to 4 of 4

Thread: Converting Images to Base64 and Back Again

  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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Converting Images to Base64 and Back Again

    I'm going to have to take issue with a few things here.

    1. Every database that I've worked with allows you to store binary data. As such, while you can't store an Image object as is in a database, you can convert that Image to a Byte array and save that. That means that your conversions between Image and Byte array and vice versa are useful for the stated purpose but the conversions to and from a base-64 String are not only superfluous but actually produce data that takes up more space than the equivalent Byte array.

    2. I hate inconsistency in code. You are using Image in some places and System.Drawing.Image in others. That's not going to do any harm in the code itself but someone who doesn't know any better might look at that and think that they are two different things because they could reasonably assume that if they were the same thing then they wouldn't be written differently for no apparent reason.

    3. In several places you are passing parameters ByRef for no good reason. If your parameter is a reference type then the one and only reason that you declare it ByRef is that you want to assign a value to that parameter inside the method and have that change reflected in the original reference. You aren't assigning anything to any parameters in any of those methods so none of those parameters should be ByRef. ByVal is the default for a reason in VB.NET and you should only ever use ByRef if you specifically need to, which you don't in this case.

    4. You have several functions there with no declared return type. That code wouldn't even compile with Option Strict On, which it should always be unless you specifcally need it Off, which is very rare. Even with Option Strict Off, the return type of those methods defaults to Object instead of what it should be in each case.

    5. You have redundant code. You define a method that converts an Image to a Byte array and yet, in your method that converts an Image to a base-64 String, you don't call that method but rather write the same code again to convert the Image to a Byte array. It's not much code but duplication of code is bad no matter how little it is.

    6. You're creating disposable objects and not disposing them. If you create an object that can be disposed and only use it in one block then create it with a Using statement so that it gets disposed implicitly. Otherwise, make sure that you call its Dispose method explicitly when you finish with it.

    This is what your code should look like:
    vb.net Code:
    1. Public Shared Function ImageToByteArray(img As Image) As Byte()
    2.     Using strm As New MemoryStream
    3.         img.Save(strm, img.RawFormat)
    4.  
    5.         Return strm.GetBuffer()
    6.     End Using
    7. End Function
    8.  
    9. Public Shared Function ImageToBase64(img As Image) As String
    10.     Return Convert.ToBase64String(ImageToByteArray(img))
    11. End Function
    12.  
    13. Public Overloads Shared Function ByteArrayToImage(data As Byte()) As Image
    14.     Using strm As New MemoryStream(data)
    15.         Return Image.FromStream(strm)
    16.     End Using
    17. End Function
    18.  
    19. Public Overloads Shared Function Base64ToImage(text As String) As Image
    20.     Return ByteArrayToImage(Convert.FromBase64String(text))
    21. End Function
    Note that, while converting an Image to base-64 text is unnecessary and counter-productive when storing the data in a database, in is still useful in cases where a Byte array cannot be stored either. For example, I answered a question just yesterday about storing an Image in My.Settings and I provided code there to convert the Image to a base-64 String and back again because a String can be saved to My.Settings.

  3. #3

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

    Re: Converting Images to Base64 and Back Again

    jmcilhinney,

    Thank you for your post. I would take offense if I hadn't dealt with your "NO BS" posting manner previously. I wrote the class out because my work deals with Base64 strings. They don't necessarily like using arrays of bytes. I prefer bytes, but threw the other methods in there for them. You are absolutely right that I should have just used the word Image instead of System.Drawing.Image. This can cause confusion and that's not my goal.

    My goal was to help people learn and to provide some code that will help them accomplish something they were setting out to do. I am still learning. Your post is very informative and concise. With your permission, I would like to re-use the code you have and update my original post. Let me know what your thoughts are. I would rather people see it from the get-go than follow posts and make changes as they read.
    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Converting Images to Base64 and Back Again

    Quote Originally Posted by mbutler755 View Post
    I would take offense if I hadn't dealt with your "NO BS" posting manner previously.
    Quote Originally Posted by mbutler755 View Post
    With your permission, I would like to re-use the code you have and update my original post. Let me know what your thoughts are. I would rather people see it from the get-go than follow posts and make changes as they read.
    Anything I post in the public domain is free to use in any way that you see fit. I don't ask for any specific credit but I do ask that you don't try to take credit for the code yourself. Beyond that it's there to help anyone who can benefit from it.

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
  •  



Click Here to Expand Forum to Full Width