1

I am trying as follows

app.component.ts

import { Component } from '@angular/core';
import { ConfigService } from './config.service';
import { SettingsService } from './settings.service';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Test display image';
  imgbytData: any;
  imageBytes: Uint8Array;
  backgroundimg: any;

  constructor(private sanitizer: DomSanitizer
  ) {}

  ngOnInit() {
    this.config.getData().subscribe((baseImage: any) => {

      this.backgroundimg = this.sanitizer.bypassSecurityTrustUrl('data:image/jpeg;base64,' + baseImage.backgroundimg);
      this.imageBytes = new Uint8Array(baseImage.image);

      const base64String = btoa(String.fromCharCode(baseImage.image));
      this.sampleimg = `url(data:image/jpeg;base64,${base64String})`;
      console.log(this.sampleimg);
    });
  }
}

app.component.html

<hello name="{{ name }}"></hello>
<img id="myimage" [src]="backgroundimg" width="100px" height="70px"/>

<div  [style.background-image]="sampleimg">
  This is new DIV Tag  
</div>

config.json file contains hard coded JSON data for image byte array. enter image description here

The problem is with <Img> tag it is rendering, but while using in style tag, it is not showing image data.

3
  • where are you declaring your config object ?! Commented Apr 2, 2023 at 13:37
  • I kept in same project, if can see in the image. for now testing purpose kept very simple, plan is to next integrate it with backend API Commented Apr 2, 2023 at 13:39
  • @DEV stackblitz.com/edit/… Commented Apr 2, 2023 at 13:42

1 Answer 1

1

You can play with CSS to get right size :

HTML

<hello name="{{ name }}"></hello>

<div [style.background-image]="'url(' + myImage + ')'"></div>

CSS :

p {
  font-family: Lato;
}

div {
  height: 500px;
  width: 500px;
}

TS :

import { Component } from '@angular/core';
import { ConfigService } from './config.service';
import { SettingsService } from './settings.service';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Test display image';
  myImage;

  constructor(
    private config: ConfigService,
    public setting: SettingsService,
    private sanitizer: DomSanitizer
  ) { }

  ngOnInit() {
    this.config.getData().subscribe((baseImage: any) => {
      this.myImage = 'data:image/jpeg;base64,' + baseImage.backgroundimg;
    });
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.