c# API接收Base64轉圖片

薛定諤家的貓發表於2018-07-24
/// <summary>
/// API接收Base64轉圖片
/// </summary>
/// <param name="Img">圖片位元組</param>
/// <param name="Path">儲存地址</param>
/// <returns></returns>
public IHttpActionResult Index(String Img, String Path)
{
//轉圖片
byte[] bit = Convert.FromBase64String(Img);
MemoryStream ms = new MemoryStream(bit);
Bitmap bmp = new Bitmap(ms);
bmp.Save(HttpContext.Current.Server.MapPath(Path) + ".jpg", ImageFormat.Jpeg);
return Ok();
}
        /// <summary>
        /// API接收路徑圖片轉Base64
        /// </summary>
        /// <param name="ImagePath">圖片地址</param>
        /// <returns></returns>
        public IHttpActionResult Index(String ImagePath)
        {
            Bitmap bmp = new Bitmap(HttpContext.Current.Server.MapPath(ImagePath));
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            return Ok(arr);
        }

 

相關文章