var img = "imgurl"//imgurl 就是你的图片路径
function getBase64Image(img) {
var canvas = document.createElement("canvas")
canvas.width = img.width
canvas.height = img.height
var ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0, img.width, img.height)
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase()
var dataURL = canvas.toDataURL("image/"+ext)
return dataURL
}
var image = new Image()
image.src = img
image.onload = function(){
var base64 = getBase64Image(image)
console.log(base64)
}
string base64Str = "图片的BASE64字符串"byte[] bytes = System.Convert.FromBase64String(base64Str)
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
{
return System.Drawing.Image.FromStream(ms)
}
<img>标签中src属性表示要显示的图片的路径。首先把图片添加到项目中,然后src中正确填写相对于当前页面图片的相对路径,就可以显示了。width和height属性可以调整图片显示大小。