gpt4 book ai didi

image - 如何在Delphi中将像素绘制到pf1bit?

转载 作者:行者123 更新时间:2023-12-03 15:48:39 24 4
gpt4 key购买 nike

如何以 pf1bit 位图格式将像素绘制到 TImage?我尝试过,但结果是整个图像都是黑色的

这是我尝试过的代码:

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;

请注意,图像大小为 320x240 像素。

先谢谢了。

最佳答案

对于 1 位颜色格式,您需要将 8 个像素打包到一个字节中。内部循环如下所示:

var
bm: TBitmap;
i, j: Integer;
dest: ^Byte;
b: Byte;
bitsSet: Integer;
begin
bm := TBitmap.Create;
Try
bm.PixelFormat := pf1bit;
bm.SetSize(63, 30);
for i := 0 to bm.Height-1 do begin
b := 0;
bitsSet := 0;
dest := bm.Scanline[i];
for j := 0 to bm.Width-1 do begin
b := b shl 1;
if odd(i+j) then
b := b or 1;
inc(bitsSet);
if bitsSet=8 then begin
dest^ := b;
inc(dest);
b := 0;
bitsSet := 0;
end;
end;
if b<>0 then
dest^ := b shl (8-bitsSet);
end;
bm.SaveToFile('c:\desktop\out.bmp');
Finally
bm.Free;
End;
end;

输出如下所示:

enter image description here

更新

Rob 的评论促使我考虑使用 Pixels[] 而不是上面的小玩意。事实上这是完全可能的。

var
bm: TBitmap;
i, j: Integer;
Color: TColor;
begin
bm := TBitmap.Create;
Try
bm.PixelFormat := pf1bit;
bm.SetSize(63, 30);
for i := 0 to bm.Height-1 do begin
for j := 0 to bm.Width-1 do begin
if odd(i+j) then begin
Color := clBlack;
end else begin
Color := clWhite;
end;
bm.Canvas.Pixels[j,i] := Color;
end;
end;
bm.SaveToFile('c:\desktop\out.bmp');
Finally
bm.Free;
End;
end;

由于每次调用分配 Pixels[] 都会导致调用 Windows API 函数 SetPixel,因此位调整代码的性能会更好。当然,只有当您的位图创建代码是性能热点时,这才有意义。

关于image - 如何在Delphi中将像素绘制到pf1bit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11375089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com