Skip to content Skip to sidebar Skip to footer

Error When Drawing Canvas Picture From Base 64 String

I want to draw a base64 String into a canvas bur I always get the error TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElem

Solution 1:

drawImage expects its first argument to be an image, video, or canvas (i.e., HTMLImageElement, HTMLVideoElement, or HTMLCanvasElement), not a string.

You need to construct a new image, and then supply that new image object with your base64 source. Once the image loads, you can add the image to your canvas:

var img = newImage();
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUh...";
img.onload = function() {
    ctx.drawImage(img, 0, 0);
}

Solution 2:

This solution is best

  1. Approach: FileReader fetch

https://stackoverflow.com/a/20285053/296373

Post a Comment for "Error When Drawing Canvas Picture From Base 64 String"