1

我对编码很陌生,所以我只是将教程中的所有内容拼凑在一起。

无论如何,当我尝试在某些大型 .png 文件上使用 Canvas.loadImage() 时,它们在视觉上会损坏。.jpgs 不会发生这种情况,它只会发生在我从 discord 下载的这些特定图像上。只有当它们超过一兆字节时。

如果我在绘画中打开一个新图像并粘贴原始图像,并将其保存在 .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,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,2778); //the size doesn't matter, it breaks no matter what
  const ctx = imgCanv.getContext("2d");
  const img = new Canvas.Image();
  img.onload = () => ctx.drawImage(img, 0, 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();

我不能发布图片,所以这里是链接:

https://user-images.githubusercontent.com/73259768/112240357-45470080-8c9c-11eb-9cc1-9af4d80bb16f.png

https://user-images.githubusercontent.com/73259768/112240416-5ee84800-8c9c-11eb-8112-f89a5fc28617.png

(我尝试使用示例图像,但它不会损坏,即使我遵循完全相同的程序。它必须是 Discord 正在做的事情,或者是原始屏幕截图中的事情。不知道。)

4

0 回答 0