After searching extensively, I came across many examples that didn't work for me. I'm not sure what I'm doing wrong and I need assistance!
I noticed that I can see the image if I use the <img>
tag, but not when I try to set it as a background-image
. However, I can view the binary image when inspecting the element.
As an attempt to solve the issue, I created an example in StackBlitz: Example on StackBlitz
Here is the 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 work -->
<div class="row" style="max-width: 100%; margin-left: 1px;" [style.background-image]="image"> </div>
And here is the TypeScript code:
@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})`);
});
}