wpfwannabe
wpfwannabe

Reputation: 14877

Get binary image content from <IMG>

I need to transfer image byte contents from JavaScript to a COM component. I will figure out the COM part but how do I get the binary image having a reference to <IMG>?

I need to support IE6+ only.

I am free to re-download the image if necessary given the source. It is not necessary to use the already downloaded image.

Is there a downloadToBytes() function or similar?

Upvotes: 1

Views: 2815

Answers (1)

jscripter
jscripter

Reputation: 840

The only thing I could think of is the use of ajax(redownloading the image)

var src = document.getElementById('theImage').src;

var ajax = new XMLHttpRequest();
ajax.open("GET", src, true);
ajax.responseType = "arraybuffer";

ajax.onload = function () {
    var bAr = new Uint8Array(ajax.response);
    for (var i = 0; i < bAr.length; i++) {  
        //Modify binary?
    }
}

ajax.send();

The only bad thing if that the image has to have cross domain permissions if it's external. Or you can execute it from a content script which doesn't have that limit(website page must be included in the permissions value in the manifest)

Upvotes: 2

Related Questions