Reputation: 1510
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
. 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
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:
@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})`);
});
}
Upvotes: 4
Views: 7379
Reputation: 8478
It works fine when i tried your stackblitz . You need to Fix CSS to get the image displayed on view.
Do not use bypassSecurityTrustUrl
for Style 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:
<div class="row"
style="width: 100%; height: 100%; position:absolute;
margin-left: 1px; background-repeat: no-repeat;"
[style.background-image]="image"> </div>
Upvotes: 7
Reputation: 93
if you try to set the background image using 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
Upvotes: 0
Reputation: 854
You can just put the URL image on src even if it's on base64
<img id="dashboardImage" [src]='objectURL' height='500' width='500' />
Upvotes: -1