简体   繁体   English

如何在 Angular 中获取图像的实际大小并调整大小?

[英]How to get real size of image and resized in Angular?

There is an image:有一张图:

@ViewChild('image')
readonly image: ElementRef;

Html: Html:

 <img #image class="image" />

How to get real size (width, height) and resized size after autofit by CSS? CSS自动调整后如何获得实际尺寸(宽度,高度)和调整大小?

I have tried this:我试过这个:

  console.log(this.container.nativeElement.clientHeight);
  console.log(this.container.nativeElement.clientWidth);

It returns me 500 and 0.它返回我 500 和 0。

try this:尝试这个:

<img #image src="images/path" class="image" (load)="onLoad()" />

in controller在 controller

onLoad() {
  console.log((this.image.nativeElement as HTMLImageElement).width);
  console.log((this.image.nativeElement as HTMLImageElement).height);
}

demo: https://stackblitz.com/edit/angular-rqceml?file=src/app/app.component.ts演示: https://stackblitz.com/edit/angular-rqceml?file=src/app/app.component.ts

You can try with this:你可以试试这个:

 console.log((this.container.nativeElement as HTMLImageElement).naturalWidth); console.log((this.container.nativeElement as HTMLImageElement).naturalHeight);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM