从base64获取图像高度和宽度GoJS中的图像字符串

问题描述 投票:1回答:1

我试图从图像的base64字符串表示获取图像的高度和宽度,我正在使用GoJS。

我最终想要做的是使用jspdf库将该图像添加到PDF。

这是我到目前为止所做的:

download() {
    let imgData = this.diagram.makeImageData({background: "white", returnType: "string"}); 
    let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF  
      var position = 0;
      var imgWidth = imgData.width; //Reading width from Image Data
      var imgHeight = imgData.height; //Reading height from Image Data
      pdf.setFontSize(30);
      pdf.text("Diagram", 100, 20, 'center');
      pdf.addImage(imgData, 'PNG', 1, 25, position, imgWidth, imgHeight);  
      pdf.save("Diagram.pdf');
  }

在imgData.width和imgData.height中,visual studio intellisense显示“属性'高度','宽度'在类型'字符串'上不存在。

如何获得图像的高度和宽度属性?

javascript angular base64 gojs
1个回答
1
投票

使用该图像数据创建HTMLImageElement,然后在加载后获取其naturalWidth和naturalHeight:

var imgData = myDiagram.makeImageData(. . .);
var img = document.createElement("img");
img.addEventListener('load', function(e) {
  console.log(img.naturalWidth + " " + img.naturalHeight);
});
img.src = imgData;
© www.soinside.com 2019 - 2024. All rights reserved.