Converts a browser File object into ImageData.
This utility handles the full pipeline of image decoding using hardware-accelerated
APIs createImageBitmap and OffscreenCanvas. It ensures that underlying
resources like ImageBitmap are properly closed even if the conversion fails.
Parameters
file: File|null|undefined
The image file to convert. Can be null or undefined.
Returns Promise<ImageData|null>
A Promise resolving to the pixel data as ImageData,
or null if no file was provided.
Throws
UnsupportedFormatError
Thrown if the provided file's MIME type does not start with image/.
Example
try { constimageData = awaitfileToImageData(file); if (imageData) { console.log('Pixels:', imageData.data); } } catch (err) { if (errinstanceofUnsupportedFormatError) { // Handle bad file type } }
Converts a browser File object into ImageData. This utility handles the full pipeline of image decoding using hardware-accelerated APIs createImageBitmap and OffscreenCanvas. It ensures that underlying resources like
ImageBitmapare properly closed even if the conversion fails.