Click here to Skip to main content
15,884,425 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.3K   1.8K   39  
A simple utility which provides a GUI to converte images to base-64 strings
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace BitmapToBase64
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        public string FileToBase64String(string path)
        {
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);

            MemoryStream memory = new MemoryStream(buffer);
            string base64 = Convert.ToBase64String(memory.ToArray());
            memory.Close();

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

            return base64;
        }
        public string IconToBase64String(Icon image)
        {
            MemoryStream memory = new MemoryStream();
            image.Save(memory);
            string base64 = Convert.ToBase64String(memory.ToArray());
            memory.Close();

            return base64;
        }

        // The following methods can be used to reverse this process.
        public Image ImageFromBase64String(string base64)
        {
            MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
            Image result = Image.FromStream(memory);
            memory.Close();

            return result;
        }
        public Icon IconFromBase64String(string base64)
        {
            MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64)); 
            Icon result = new Icon(memory);
            memory.Close();
            
            return result;
        }
        public Cursor CursorFromBase64String(string base64)
        {
            MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
            Cursor result = new Cursor(memory);
            memory.Close();

            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "All Supported Files (*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png;*.ico;*.cur)|*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png;*.ico;*.cur|Image Files (*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png|Icon Files (*.ico;*.cur)|*.ico;*.cur|All Files (*.*)|*.*";

            if (openDlg.ShowDialog(this) == DialogResult.OK)
            {
                Cursor = Cursors.WaitCursor;

                try
                {
                    txtCurrentPath.Text = openDlg.FileName;
                    pictureBox1.Image = Image.FromFile(openDlg.FileName);
                    pictureBox1.Visible = true;
                }
                catch (Exception)
                {
                    pictureBox1.Image = null;
                    pictureBox1.Visible = false;
                }

                lblNotImage.Visible = !pictureBox1.Visible;

                try
                {
                    txtBase64.Text = FileToBase64String(openDlg.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show(this, string.Format("An error occurred whilst converting to base-64."),
                        Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Cursor = Cursors.Default;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(txtBase64.Text);
            }
            catch (Exception)
            {
                MessageBox.Show(this, string.Format("Was unable to copy to clipboard."),
                    Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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