Use the following code, Successful outputĀ
But I feel very troublesomeļ¼Someone can provide a QM function? I didn't find any relevant information in the entire forum.
Macro IMG2Base64
But I feel very troublesomeļ¼Someone can provide a QM function? I didn't find any relevant information in the entire forum.
Macro IMG2Base64
CsScript x
x.AddCode("")
str IMG64 = x.Call("ImgToBase64String" "c:\OCR test.png")
out IMG64
#ret
;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);
;;;;;;;;;}
;;;;;}
;}