How to draw pixels to a TImage but in pf1bit bitmap format? I have tried but the result is the whole of the image are black.
here is the code I’ve tried :
image1.picture.bitmap.loadfromfile('example.bmp'); // an image which is RGB pf24bit with 320 x 240 px resolution
image1.picture.bitmap.pixelformat := pf1bit;
for i:=0 to round(image1.picture.bitmap.canvas.height/2) - 1 do
begin
for j:=0 to round(image1.picture.bitmap.canvas.width/2) - 1 do
begin
image1.picture.bitmap.Canvas.pixels[i,j]:=1; // is this correct? ... := 1? I've tried to set it to 255 (mean white), but still get black
end;
end;
note that the image size is 320×240 pixel.
Thanks before.
You need to pack 8 pixels into a single byte for 1 bit color format. The inner loop would look like this:
The output looks like this:
Update
Rob’s comment prompted me to look at using
Pixels[]rather than the bit-twiddling above. And indeed it is perfectly possible.Since each call to assign
Pixels[]results in a call to the Windows API functionSetPixel, the bit-twiddling code would perform better. Of course, that would only ever matter if your bitmap creation code was a performance hot-spot.