Support Portal

for ProcessRobot and WinAutomation

Start a new topic
Planned

call function from the Dll file

Calling a function from an external dll is a very useful feature


I need to use C# function ImgToBase64String to convert the image to Base64 format. Can someone provide an example? Thanks in advance


C# code:

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Drawing.Imaging;

namespace Html5Image.Tools
{
    public class ImageTool
    {
        //Image to base64 encoded text
        public static string ImgToBase64String(string Imagefilename)
        {
            Bitmap bmp = new Bitmap(Imagefilename);
            //this.pictureBox1.Image = bmp;
            //FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);
            //StreamWriter sw = new StreamWriter(fs);

            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            String strbaser64 = Convert.ToBase64String(arr);

            return strbaser64;
        }

        public static Bitmap Base64StringToImage(string base64Img)
        {
            byte[] bytes = Convert.FromBase64String(base64Img);
            MemoryStream ms = new MemoryStream();
            ms.Write(bytes, 0, bytes.Length);
            Bitmap bmp = new Bitmap(ms);

            return bmp;
        }

        /// <summary>
        /// save Picture
        /// </summary>
        /// <param name="base64Img"></param>
        /// <param name="imgPath"></param>
        /// <param name="imgFormat">EX: System.Drawing.Imaging.Jpeg </param>
        public static void SaveFile(string base64Img, string imgPath, ImageFormat imgFormat )
        {
            string dir = Path.GetDirectoryName(imgPath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var bitmap = Base64StringToImage(base64Img); 
            bitmap.Save(imgPath, imgFormat); 
        }
    }
}

 

Maybe you can solve this using ImageMagick library and executing with the base64 option from command line.

Login or Signup to post a comment