【问题标题】:Gain the length and width of the image in angular (from component)以角度获取图像的长度和宽度(来自组件)
【发布时间】:2019-11-22 15:00:34
【问题描述】:

我想获取我上传的图片的长度和宽度,它的base64是。 这是我的代码:

Image img = new Image();
img.src = Selectedimage.src;
Img.onload = function {
this.width = img.width;
this.height = img.height; }

问题是我无法通过 «this» 访问此类中的变量。 有人知道问题出在哪里,或者有其他方法吗?

【问题讨论】:

标签: html angular image typescript width


【解决方案1】:

问题的原因是您无法在 Img.onload 函数中使用 this 访问 widthheight,因为它位于另一个 scope 中。尝试如下:

var self = this;
Img.onload = function {
    self.width = img.width;
    self.height = img.height; 
}

【讨论】:

  • 用更多细节更新您的问题。我的意思是更多的代码。如果你能创建 stackblitz 就更好了
  • 我又查了一下,回复了……问题是它本身不返回长宽……要换一种方式得到长宽?图片格式也是Base64。
猜你喜欢
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 2011-11-26
  • 1970-01-01
相关资源
最近更新 更多