问题描述
我对编码非常陌生,所以我只是将教程中的所有内容拼凑在一起。
无论如何,当我尝试在某些大型 .png 文件上使用 Canvas.loadImage() 时,它们在视觉上已损坏。 它不会发生在 .jpgs 上,它只会发生在我从 discord 下载的这些特定图像上。并且仅当它们超过 1 兆字节时。
如果我在 Paint 中打开一个新图像并粘贴原始图像,并将其保存为 .png,我可以运行该图像,并且它不会损坏。 那么有人知道它为什么会腐败吗?如果这不是我正在做的事情,我如何即时重新编码它们以使其不会损坏。
我尝试了两种/三种不同的方法,它们都产生了相同的结果:
首先作为回调(这是正确的术语吗?)?:
function test(){
const imgCanv = Canvas.createCanvas(1284,2778); //the size doesn't matter,it breaks no matter what
const ctx = imgCanv.getContext("2d");
const img = Canvas.loadImage("image.png");
img.then((img) => {
ctx.drawImage(img,0);
var out = fs.createWriteStream("result.png")
var stream = imgCanv.pngStream();
stream.on("data",function(chunk){
out.write(chunk);
});
stream.on("end",function(){
console.log("Success");
});
}).catch(err =>{
console.log("An error occured:" + err);
})
}
test();
还有他们在文档中建议的方式,使用 img.onload:
function test(){
const imgCanv = Canvas.createCanvas(1284,it breaks no matter what
const ctx = imgCanv.getContext("2d");
const img = new Canvas.Image();
img.onload = () => ctx.drawImage(img,0);
img.onerror = err => { throw err };
img.src = "image.png";
var out = fs.createWriteStream("result.png")
var stream = imgCanv.pngStream();
stream.on("data",function(chunk){
out.write(chunk);
});
stream.on("end",function(){
console.log("Success");
});
}
test();
(我尝试使用示例图像,但它不会损坏,即使我遵循完全相同的步骤。它必须是 discord 正在执行的操作,或者原始屏幕截图中的某些内容。不知道。)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)