Click here to Skip to main content
6,822,613 members and growing! (16,023 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate License: The GNU Lesser General Public License

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

By lhayes00

A simple utility which provides a GUI to converte images to base-64 strings
C#3.0, Windows, .NET3.0, ASP.NET, GDI+, Dev
Posted:31 May 2008
Updated:11 Sep 2008
Views:22,588
Bookmarked:24 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.76 Rating: 3.06 out of 5
1 vote, 12.5%
1
4 votes, 50.0%
2

3
1 vote, 12.5%
4
2 votes, 25.0%
5
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

About the Author

lhayes00


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.
Location: United Kingdom United Kingdom

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralHow to download Pinmemberkobsev20:35 22 Jun '09  
GeneralRe: 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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 11 Sep 2008
Editor: Sean Ewington
Copyright 2008 by lhayes00
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project