Click here to Skip to main content
Licence LGPL3
First Posted 31 May 2008
Views 36,346
Downloads 477
Bookmarked 34 times

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

By Lea Hayes | 11 Sep 2008
A simple utility which provides a GUI to converte images to base-64 strings
1 vote, 12.5%
1
4 votes, 50.0%
2

3
1 vote, 12.5%
4
2 votes, 25.0%
5
3.06/5 - 8 votes
μ 3.06, σa 2.72 [?]
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)

About the Author

Lea Hayes

Web Developer
Rotorz Limited
United Kingdom United Kingdom

Member
I have been using computers since I was about 8 years old, and have always enjoyed studying new concepts and technologies. I have been programming since about 1995 and have become experienced with many programming languages and technologies including C/C++, C#, DirectX, OpenGL, Java, ASP.NET, XML, XSL, SQL, JavaScript, HTML, CSS, ActionScript, VB, and Conitec Game Studio A5 Pro.
 
I have always been interested in game and multimedia development and have recently graduated with the first-class degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich.
 
Since graduating I have been continuing to study software and game development. In particular I am interested in game engine creation and programming language development. In addition to study, I am working on the design and creation of a game engine.
 
In my spare time I enjoy playing my musical instruments, especially my guitars. Its great fun and something which helps me unwind after a long day. I enjoy many flavours of music, in particular rock and heavy metal bands like Megadeth, Anacrusis, and Pink Floyd.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to download Pinmemberkobsev20:35 22 Jun '09  
AnswerRe: How to download Pinmemberlhayes003:36 23 Jun '09  
GeneralNifty little tool [modified] Pinmemberwwwastrel0:01 19 Mar '09  
GeneralRe: Nifty little tool Pinmemberlhayes0010:59 19 Mar '09  
GeneralRe: Nifty little tool Pinmemberwwwastrel13:59 19 Mar '09  
Generalxml PinmemberVakond Jeno5:52 24 Sep '08  
GeneralRe: xml Pinmemberlhayes0017:04 24 Sep '08  
GeneralA problem ! PinmemberMohammad Dayyan11:58 15 Sep '08  
GeneralRe: A problem ! Pinmemberlhayes0012:03 15 Sep '08  
GeneralRe: A problem ! [modified] PinmemberMohammad Dayyan12:17 15 Sep '08  
GeneralRe: A problem ! Pinmemberlhayes0023:47 16 Sep '08  
GeneralRe: A problem ! PinmemberMohammad Dayyan3:01 17 Sep '08  
GeneralRe: A problem ! Pinmemberlhayes003:11 17 Sep '08  
GeneralRe: A problem ! PinmemberMohammad Dayyan3:17 17 Sep '08  
GeneralExcellent work PinmemberMohammad Dayyan4:22 12 Sep '08  
GeneralThank you , but : PinmemberMohammad Dayyan13:50 9 Sep '08  
GeneralRe: Thank you , but : Pinmemberlhayes0013:54 9 Sep '08  
GeneralRe: Thank you , but : PinmemberMohammad Dayyan14:01 9 Sep '08  
GeneralRe: Thank you , but : Pinmemberlhayes0016:13 9 Sep '08  
GeneralRe: Thank you , but : PinmemberMohammad Dayyan16:21 9 Sep '08  
GeneralRe: Thank you , but : Pinmemberlhayes001:31 10 Sep '08  
GeneralRe: Thank you , but : PinmemberMohammad Dayyan1:37 10 Sep '08  
GeneralYeah! Thanks Pinmemberconrad Braam23:18 31 May '08  

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.

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