using System;
using System.Drawing;
using System.IO;

namespace UtilsHelper
{
    class Utils
    {
        public string ImgToBase64String(string imagePath)
        {
            try
            {
                Bitmap bmp = new Bitmap(imagePath);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                Console.WriteLine("图片转basse64编码字符串失败：" + ex);
                return null;
            }
        }
    }
}
