Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
3.20/5 (11 votes)
11 Sep 2008MIT1 min read 84.2K   1.8K   39   24
A simple utility which provides a GUI to converte images to base-64 strings

image-base-64-converter/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

C#
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

C#
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 MIT License


Written By
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/)

Comments and Discussions

 
QuestionI cannot execute the app Pin
linfengtingyu18932323224-Jun-13 4:25
linfengtingyu18932323224-Jun-13 4:25 
QuestionHow to download Pin
kobsev22-Jun-09 19:35
kobsev22-Jun-09 19:35 
AnswerRe: How to download Pin
Lea Hayes23-Jun-09 2:36
Lea Hayes23-Jun-09 2:36 
GeneralNifty little tool [modified] Pin
wwwastrel18-Mar-09 23:01
wwwastrel18-Mar-09 23:01 
GeneralRe: Nifty little tool Pin
Lea Hayes19-Mar-09 9:59
Lea Hayes19-Mar-09 9:59 
GeneralRe: Nifty little tool Pin
wwwastrel19-Mar-09 12:59
wwwastrel19-Mar-09 12:59 
Correct.

The old "default" line length for MIME is 76 (which I presume is still true, though not relevant for my purpose beyond the fact that the encoding tool I've been using--an 'online' tool--does use this default) and for something else (which I don't use so don't remember) is 64. One of the editors I use doesn't have a function for wrapping the line, so when I insert base64 encoded text it extends far beyond the right margin, which also happens with several processes used just to view the code. Allowing for those processes by limiting line length to the old MIME standard of 76 is really just more of convenience factor for my usage; I only mentioned it at all on the off chance that you might be looking for another feature to add, another reason to "tinker" Big Grin | :-D .

Thanks again.
Generalxml Pin
Vakond Jeno24-Sep-08 4:52
Vakond Jeno24-Sep-08 4:52 
GeneralRe: xml Pin
Lea Hayes24-Sep-08 16:04
Lea Hayes24-Sep-08 16:04 
GeneralA problem ! Pin
Mohammad Dayyan15-Sep-08 10:58
Mohammad Dayyan15-Sep-08 10:58 
GeneralRe: A problem ! Pin
Lea Hayes15-Sep-08 11:03
Lea Hayes15-Sep-08 11:03 
GeneralRe: A problem ! [modified] Pin
Mohammad Dayyan15-Sep-08 11:17
Mohammad Dayyan15-Sep-08 11:17 
GeneralRe: A problem ! Pin
Lea Hayes16-Sep-08 22:47
Lea Hayes16-Sep-08 22:47 
GeneralRe: A problem ! Pin
Mohammad Dayyan17-Sep-08 2:01
Mohammad Dayyan17-Sep-08 2:01 
GeneralRe: A problem ! Pin
Lea Hayes17-Sep-08 2:11
Lea Hayes17-Sep-08 2:11 
GeneralRe: A problem ! Pin
Mohammad Dayyan17-Sep-08 2:17
Mohammad Dayyan17-Sep-08 2:17 
GeneralExcellent work Pin
Mohammad Dayyan12-Sep-08 3:22
Mohammad Dayyan12-Sep-08 3:22 
GeneralThank you , but : Pin
Mohammad Dayyan9-Sep-08 12:50
Mohammad Dayyan9-Sep-08 12:50 
GeneralRe: Thank you , but : Pin
Lea Hayes9-Sep-08 12:54
Lea Hayes9-Sep-08 12:54 
GeneralRe: Thank you , but : Pin
Mohammad Dayyan9-Sep-08 13:01
Mohammad Dayyan9-Sep-08 13:01 
GeneralRe: Thank you , but : Pin
Lea Hayes9-Sep-08 15:13
Lea Hayes9-Sep-08 15:13 
GeneralRe: Thank you , but : Pin
Mohammad Dayyan9-Sep-08 15:21
Mohammad Dayyan9-Sep-08 15:21 
GeneralRe: Thank you , but : Pin
Lea Hayes10-Sep-08 0:31
Lea Hayes10-Sep-08 0:31 
GeneralRe: Thank you , but : Pin
Mohammad Dayyan10-Sep-08 0:37
Mohammad Dayyan10-Sep-08 0:37 
GeneralYeah! Thanks Pin
conrad Braam31-May-08 22:18
conrad Braam31-May-08 22:18 

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

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