• Convert File or Blob to HTMLImageElement.

    Creates an object URL from the file/blob and loads it as an image. The object URL is automatically cleaned up after loading.

    Parameters

    • file: File | Blob

      The File or Blob to convert to an image

    Returns Promise<HTMLImageElement>

    Promise that resolves to an HTMLImageElement

    Throws

    When image loading fails (invalid format, corrupted data, etc.)

    Example

    // Convert file input to image
    const fileInput = document.getElementById('file') as HTMLInputElement;
    const file = fileInput.files[0];
    const image = await fileToImage(file);

    // Convert blob to image
    const response = await fetch('image.jpg');
    const blob = await response.blob();
    const image = await fileToImage(blob);