How to view image with Angular 2+

Asked

Viewed 2,624 times

1

I have the code below where I need to display an image coming from the database, the image is coming here as byte how to display this image on the screen?

  <!-- Listar As Bebidas Alcoólicas -->
  <div class="card-columns" *ngIf="bebidasAlcoolicas != null">
    <div class="card" *ngFor="let bebidaAlcoolica of bebidasAlcoolicas">
      <button type="button" class="btn btn-danger" (click)="incluirCarrinhoBebidaAlcoolica(bebidaAlcoolica)">
        <img class="card-img-top" src="bebidaAlcoolica.imagemBebidaAlcoolica" alt="Card image cap">
      </button>
      <div class="card-body">
        <h5 class="card-title text-primary font-weight-bold">{{bebidaAlcoolica.nomeBebidaAlcoolica}}</h5>
        <p class="card-text text-danger">{{bebidaAlcoolica.valorBebidaAlcoolica | currency:'BRL':true}}</p>
      </div>
    </div>
  </div>

Attempt 2:

request-piece.create-Component.ts

    loadImagem(encryptedImage) {
        this.imageData = 'data:image/png;base64,' + encryptedImage;
        this.sanitizedImageData = this.sanitizer.bypassSecurityTrustUrl(this.imageData);
        console.log(this.sanitizedImageData);
    }

standalone.create-Component.html

  <!-- Listar As Bebidas Alcoólicas -->
  <div class="card-columns" *ngIf="bebidasAlcoolicas != null">
    <div class="card" *ngFor="let bebidaAlcoolica of bebidasAlcoolicas">
      <button type="button" class="btn btn-danger" (click)="incluirCarrinhoBebidaAlcoolica(bebidaAlcoolica)">
        <img class="card-img-top" [src]='sanitizedImageData' *ngIf="loadImagem(bebidaAlcoolica.imagemBebidaAlcoolica)" alt="Card image cap">
      </button>
      <div class="card-body">
        <h5 class="card-title text-primary font-weight-bold">{{bebidaAlcoolica.nomeBebidaAlcoolica}}</h5>
        <p class="card-text text-danger">{{bebidaAlcoolica.valorBebidaAlcoolica | currency:'BRL':true}}</p>
      </div>
    </div>
  </div>
  • What is the language of your backend?

  • Good afternoon, my Backend is Java.

  • Angular, to render the image, requires it to be in Base64

  • It is not byte[] it arrives as a string in JSON.

  • i open this site: https://codebeautify.org/base64-to-image-converter put the string and Gero the image that is saved in the database.

1 answer

1

Angular has nothing native to render an array of bytes in image. But it can transform an encrypted byte array in Base64 into a rendered image using a directive.

I took the example of Soen, but I gave a good adapted for its use here.

The entire code will be encapsulated in our new directive, ImageBytesDirective.

import {Directive, OnInit, Input} from '@angular/core';
import {Http} from '@angular/http';
import {BROWSER_SANITIZATION_PROVIDERS, DomSanitizationService} from '@angular/platform-browser';

@Directive({
  selector: '[image-bytes]',
  providers: [BROWSER_SANITIZATION_PROVIDERS],
  host: {
    '[src]': 'sanitizedImageData'
  }
})
export class ImageBytesDirective implements OnInit {
  imageData: any;
  sanitizedImageData: any;
  @Input('image-bytes') encryptedImage: string;

  constructor(private http: Http,
              private sanitizer: DomSanitizationService) { }

  ngOnInit() {
    this.imageData = 'data:image/png;base64,' + encryptedImage;
    this.sanitzedImageData = sanitizer.bypassSecurityTrustUrl(imageData);

  }
}

To use simply add the ImageBytesDirective to its component Directives and to:

<img [image-bytes]="bebidaAlcolica.imageBase64" width="100" height="100" />
  • are not finding these Imports: import {BROWSER_SANITIZATION_PROVIDERS, Domsanitizationservice} from '@angular/Platform-browser'; have to install something?

  • At first you need @angular/Platform-browser

  • I do not know did not work to make this directive, I tried more or less to adapt this code looks like up there, but does not show the image on the screen but does not get an x in the image saying that did not find.

  • It may be because the image is corrupted or there was something in the p Base64 encryption

Browser other questions tagged

You are not signed in. Login or sign up in order to post.