简体   繁体   English

背景图像未显示在 Angular 中

[英]background image not showing in Angular

Yes, I did search and found many examples, but none worked for me.是的,我确实搜索并找到了很多示例,但没有一个对我有用。 I am not sure what am I doing wrong.我不确定我做错了什么。 Need help!需要帮忙!

I can see the image if I put in <img> tag, but not when I put as background-image .如果我放入<img>标签,我可以看到图像,但当我放入background-image时则看不到。 But I can see the binary image when I do inspect element.但是当我检查元素时,我可以看到二进制图像。

Tried doing an example in stackblitz https://stackblitz.com/edit/display-image-from-rest-api-qpnspq?embed=1&file=src/app/app.component.ts尝试在 stackblitz https://stackblitz.com/edit/display-image-from-rest-api-qpnspq?embed=1&file=src/app/app.component.ts中做一个例子

HTML: HTML:

 <!-- This works -->
 <div class="row" style="max-width: 100%; margin-left: 1px;" >
     <img id="dashboardImage" [src]='imageBlobDataUrl' height='500' width='500' />
 </div>

 <!-- This does NOT works -->
<div class="row" style="max-width: 100%; margin-left: 1px;" [style.background-image]="image"> </div>

TS: TS:

    @Input() imageBlobDataUrl: any;
    selectedImage: QiImage = new QiImage();

    public header:SafeHtml;
    public content:SafeHtml[];
    public image:SafeStyle;

  constructor(
          private qiImageService: QiImageService,
          private sanitizer: DomSanitizer,
          private imageService: ImageService
  ) {}
  populateImageDetails(lineId, bitmapFileName) {
      this.imageService
          .populateImageDetails(lineId, bitmapFileName)
              .subscribe( (selectedImage : any) => {
                  this.selectedImage = selectedImage;
                  //let TYPED_ARRAY = new Uint8Array(selectedImage.imageData);
                  //const STRING_CHAR = String.fromCharCode.apply(null, TYPED_ARRAY);
                  //let base64String = btoa(STRING_CHAR);
                  let objectURL = 'data:image/png;base64,' + selectedImage.imageData;
                  this.imageBlobDataUrl = this.sanitizer.bypassSecurityTrustUrl(objectURL);
                  this.image = this.sanitizer.bypassSecurityTrustStyle(`url(${objectURL})`);
              });
  }

在此处输入图像描述

You can just put the URL image on src even if it's on base64您可以将 URL 图像放在 src 上,即使它在 base64 上

 <img id="dashboardImage" [src]='objectURL' height='500' width='500' />

if you try to set the background image using CSS?如果您尝试使用 CSS 设置背景图像? like this:像这样:

.bg {
  background-image: url("../../yourimage");
  height: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

}

In the template you can use the binding to set the css class as you like在模板中,您可以根据需要使用绑定设置 css class

It works fine when i tried your stackblitz .当我尝试你的stackblitz时它工作正常。 You need to Fix CSS to get the image displayed on view.您需要修复 CSS 以使图像显示在视图中。

Do not use bypassSecurityTrustUrl for Style Url.不要将bypassSecurityTrustUrl用于样式 Url。 So the below for thumbnail is not valid:所以下面的缩略图是无效的:

this.thumbnail = this.sanitizer.bypassSecurityTrustUrl(objectURL); // Do NOT Use

this.image = this.sanitizer.bypassSecurityTrustStyle(`url(${objectURL})`); // This works fine and is Valid

Try to adjust the CSS position for your image and it will show up:尝试为您的图像调整CSS position ,它将显示:

<div class="row" 
  style="width: 100%; height: 100%; position:absolute; 
  margin-left: 1px;  background-repeat: no-repeat;" 
  [style.background-image]="image"> </div>

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

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