代码如下:
<!doctype html>
<html>
<body id="body">
<img id="pic">
<script src="./b.js"></script>
</body>
</html>
其中, b.js 内代码如下:
var c,ctx,p1,p2,b;
c=document.createElement("canvas");
p1=document.createElement("img");
p2=document.getElementById("pic");
b=document.getElementById("body");
b.appendChild(p1);
p1.src="./a.jpg";
p1.onload=function() {
try{
c.width=p1.width;
c.height=p1.height;
ctx=c.getContext("2d");
b.appendChild(c);
ctx.drawImage(p1,0,0);
alert("Fine 2.");
while(p2==null)alert("Error.");
alert("Fine 3.");
p2.src=c.toDataURL("image/png");
alert("Fine.");
} catch (e) {
alert(""+e);
}
}
在此文件夹下,还有一个 jpg 文件。
代码报错,报错信息如下:
SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
我想利用浏览器将 jpg 转 png ,请问报错如何解决?