jotting

C# Base64 String to JPEG Image

延續:Check size and compress the picture


Save Image

C Sharp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string path = HttpContext.Current.Server.MapPath("../img/user_share/" + guid);
try
{
byte[] bytes = Convert.FromBase64String(base64String);

System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
image.Save(path + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
return true;
}
catch (Exception ex)
{
return false;
}

Note

  1. Server.MapPath
    對應至 Web 伺服器上的實體檔案路徑 path。