Click here to Skip to main content
Click here to Skip to main content
Go to top

Image, Icon, Cursor, and Anything Else to Base-64 Converter Utility

, 11 Sep 2008
Rate this:
Please Sign up or sign in to vote.
A simple utility which provides a GUI to converte images to base-64 strings
ConvertToBase64_screenshot.png

Introduction

During a recent project I found myself needing to convert several bitmap images to base-64. I couldn't find a simple tool to achieve this, so I created this very simple utility to facilitate my task. So whilst this article only presents some basic information, its purpose is simply to assist those who could benefit from such a utility.

The latest version of this utility now supports images, icons, cursors, along with any other data file. The new version provides the following functions:

  • string FileToBase64String(string path) - Refer to ImageFromBase64String as a guide to reverse procedure.
  • string ImageToBase64String(Image image, ImageFormat format)
  • string IconToBase64String(Icon image)
  • Image ImageFromBase64String(string base64)
  • Icon IconFromBase64String(string base64)
  • Cursor CursorFromBase64String(string base64)

Background

Images can be stored either externally to your application or internally within some form of resource file. However, sometimes it is useful to store a string representation of an image. For example, when binary transfer is not an option, or if an image is to be stored directly within source code.

Using the Code

Whilst the tool is fairly self contained I thought it would be useful to explain how this utility converts from an image to base-64, and likewise how to convert back again.

Convert from an Image to a base-64 string

public string ImageToBase64String(Image image, ImageFormat format)
{
   MemoryStream memory = new MemoryStream();
   image.Save(memory, format);
   string base64 = Convert.ToBase64String(memory.ToArray());
   memory.Close();

   return base64;
}

Convert from a base-64 string to an Image

public Image ImageFromBase64String(string base64)
{
   MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
   Image result = Image.FromStream(memory);
   memory.Close();

   return result;
}

History

  • 31st May, 2008: Initial post
  • 11th Sept, 2008: updated version contains specific support for images, icons, and cursors, but it can now also convert any file to base-64

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Lea Hayes
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.
 
Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.
 
Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.
 
Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!
 
Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.
 
We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.
 
Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionI cannot execute the app Pinmemberlinfengtingyu18932323224-Jun-13 4:25 
QuestionHow to download Pinmemberkobsev22-Jun-09 19:35 
AnswerRe: How to download Pinmemberlhayes0023-Jun-09 2:36 
GeneralNifty little tool [modified] Pinmemberwwwastrel18-Mar-09 23:01 
GeneralRe: Nifty little tool Pinmemberlhayes0019-Mar-09 9:59 
GeneralRe: Nifty little tool Pinmemberwwwastrel19-Mar-09 12:59 
Generalxml PinmemberVakond Jeno24-Sep-08 4:52 
GeneralRe: xml Pinmemberlhayes0024-Sep-08 16:04 
GeneralA problem ! PinmemberMohammad Dayyan15-Sep-08 10:58 
GeneralRe: A problem ! Pinmemberlhayes0015-Sep-08 11:03 
GeneralRe: A problem ! [modified] PinmemberMohammad Dayyan15-Sep-08 11:17 
GeneralRe: A problem ! Pinmemberlhayes0016-Sep-08 22:47 
GeneralRe: A problem ! PinmemberMohammad Dayyan17-Sep-08 2:01 
GeneralRe: A problem ! Pinmemberlhayes0017-Sep-08 2:11 
GeneralRe: A problem ! PinmemberMohammad Dayyan17-Sep-08 2:17 
GeneralExcellent work PinmemberMohammad Dayyan12-Sep-08 3:22 
GeneralThank you , but : PinmemberMohammad Dayyan9-Sep-08 12:50 
GeneralRe: Thank you , but : Pinmemberlhayes009-Sep-08 12:54 
GeneralRe: Thank you , but : PinmemberMohammad Dayyan9-Sep-08 13:01 
GeneralRe: Thank you , but : Pinmemberlhayes009-Sep-08 15:13 
GeneralRe: Thank you , but : PinmemberMohammad Dayyan9-Sep-08 15:21 
GeneralRe: Thank you , but : Pinmemberlhayes0010-Sep-08 0:31 
GeneralRe: Thank you , but : PinmemberMohammad Dayyan10-Sep-08 0:37 
GeneralYeah! Thanks Pinmemberconrad Braam31-May-08 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 11 Sep 2008
Article Copyright 2008 by Lea Hayes
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid