I read a .gif file from the server into an object of type TGraphic with the LoadFromURL(URL, callback) method. In the callback function, I draw the loaded bitmap onto the canvas of a TJSCanvasRenderingContext2D context, this works fine.
My problem: I now have to manipulate the bitmap (exchange some pixels' color) before drawing. For that, I try to read the .canvas.pixels[...] array of the TGraphic object (in the callback function). But this gives only black pixels for the whole image.
And reading the image data with the method .GetBase64Image gives an empty string.
Where is this TJSCanvasRenderingContext2D coming from and what is its relationship to your canvas?
Either you use TCanvas everywhere and can use Pixels or you use JavaScript objects like TJSCanvasRenderingContext2D and continue to work at level of the JavaScript object.
My goal is to read a graphics file, then manipulate it pixel-wise (for example exchange black pixels by gray) and finally draw it onto a paintbox. To make it more clear, please see following sample code.
var
myGraphic : TGraphic;
procedure TForm1.graphcallback;
var
count, x, y : integer;
begin
count:=0;
form1.weblabel1.caption:=inttostr(myGraphic.width)+' x '+inttostr(myGraphic.height)+' pixels';
for x := 0 to myGraphic.width-1 do
for y := 0 to myGraphic.height-1 do
begin
if myGraphic.canvas.pixels[x,y]=0 then
inc(count);
end;
weblabel2.caption:='black pixels: '+inttostr(count);
end;
procedure TForm1.WebButton6Click(Sender: TObject);
begin
myGraphic.LoadFromURL('https://www.budenberg1.de/assets/gif3/KEINHUND.GIF', form1.graphcallback);
end;
initialization
myGraphic:=TGraphic.Create();
end.
When run, the code produces label1='187 x 187 pixels', which is correct. It also sets label2=''black pixels: 34969', which means that EVERY pixel is counted as a black pixel, but that's not true as you can easily see when looking at the picture.
How can I read the pixels of a TGraphic object which is loaded from the server?