DEDI RISWANDI

Minggu, 09 April 2017

Unknown

Convert an Image from Base64 and to Base64 Images Converter

C# for beginners - How to convert an image to Base64 code and/or from Base64 code into an image using C# programming language? The simple application 'Images Converter' will allow you to convert all images type to Base64 codes, we can select / browse an image where we want to convert it, just click "Convert button" and you get your Base64 code.

Create Base64 Images Converter

Just create new project in your visual studio and rename with "Base64ImagesConverter", at the form1.vb please add 6 Buttons, one TextBox, One RichTextBox and OpenFileDialog Component, and design it look like this images :

Convert an Image from Base64 and to Base64

Just focuss to our project dude xD

Source Code Base64 Images Convert (Form1.Cs)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Base64ImagesConverter {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
            DialogResult dr = openFileDialog1.ShowDialog();
            try {

                textBox1.Text = openFileDialog1.FileName;

            } catch (Exception x) {

                MessageBox.Show(x.Message);
            }
        }
        private void button2_Click(object sender, EventArgs e) {

            if (textBox1.Text == "") {

                MessageBox.Show("No path spesified");
                textBox1.Focus();
            }
            else {

                byte[] imagearray = System.IO.File.ReadAllBytes(textBox1.Text);
                string createBase64 = Convert.ToBase64String(imagearray);
                richTextBox1.Text = createBase64;
            }
        }
        private void button3_Click(object sender, EventArgs e) {

            if (richTextBox1.Text !="") {

                // we can create base64 functions
                pictureBox1.Image = Base64toImage(richTextBox1.Text);

            } else {

                MessageBox.Show("Paste Base 64 string First");
                richTextBox1.Focus();
            }
        }
        public Image Base64toImage(string base64string) {

            // Convert Base64 String to byte[]
            byte[] imagesByte = Convert.FromBase64String(base64string);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(imagesByte, 0 , imagesByte.Length);

            // Convert byte[] to Image , based on @Crulex comment, the below line has no need since MemoryStream already initialized
            ms.Write(imagesByte, 0 , imagesByte.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
        private void button4_Click(object sender, EventArgs e) {

            Clipboard.Clear();
            Clipboard.SetText(richTextBox1.Text.ToString());
            MessageBox.Show("Copied to clipboard!");
            richTextBox1.Clear();
        }
        private void button5_Click(object sender, EventArgs e) {
            richTextBox1.Text = Clipboard.GetText();
        }
        private void button6_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}

Unknown

About Unknown -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :