Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Convert Image to Base64 (Ready for HTML tag)

Description
Converts an image to Base64 ready to be used in an HTML tag.
Language
C#.net
Minimum Version
Created By
Wakewalker
Contributors
-
Date Created
Oct 17, 2017
Date Last Modified
Mar 17, 2022

Macro Code

//WARNING: this script is NOT compatible with ClipboardFusion 6.0+ because of: HttpServerUtility.UrlTokenDecode

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
using System.Web;

public static class ClipboardFusionHelper
{
    ///<summary>
    /// Base 64 Encoding with URL and Filename Safe Alphabet using UTF-8 character set.
    ///</summary>
    ///<param name="str">The origianl string</param>
    ///<returns>The Base64 encoded string</returns>
    public static string Base64ForUrlEncode(string str)
    {
        byte[] encbuff = Encoding.UTF8.GetBytes(str);
        return HttpServerUtility.UrlTokenEncode(encbuff);
    }
    ///<summary>
    /// Decode Base64 encoded string with URL and Filename Safe Alphabet using UTF-8.
    ///</summary>
    ///<param name="str">Base64 code</param>
    ///<returns>The decoded string.</returns>
    public static string Base64ForUrlDecode(string str)
    {
        byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
        return Encoding.UTF8.GetString(decbuff);
    } 
    internal static byte[] ImageToByteArray(Image img)
    {
        byte[] byteArray = new byte[0];
        MemoryStream stream = new MemoryStream();
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();
        byteArray = stream.ToArray();
        return byteArray;
    }
	public static string ProcessText(string text)
	{
		if(!Clipboard.ContainsImage())
			return text;
			
		BinaryFormatter binFormatter = new BinaryFormatter();
		using(Image img = Clipboard.GetImage())           
		{
			text = Convert.ToBase64String(ImageToByteArray(img));
		}
		
		//BFS.Dialog.ShowMessageInfo("Convert image to Base64 is finished!");
		return string.Format(@"data:image/gif;base64,{0}", text);
	}
}