我正在尝试执行以下操作:
bmp := TBitmap.Create;
bmp.Width := FWidth;
bmp.Height := FHeight;
for y := 0 to FHeight - 1 do
begin
sl := bmp.ScanLine[y];
for x := 0 to FWidth - 1 do
begin
//draw to the scanline, one pixel at a time
end;
end;
//display the image
bmp.Free;
不幸的是,最后我得到的是一张完全白色的图像,除了底线,它的颜色适当。一点调试显示,每次我访问
ScanLine
属性时,它都会调用TBitmap.FreeImage
,并进入if (FHandle <> 0) and (FHandle <> FDIBHandle) then
块,该块将重置整个图像,因此实际上只对最后一行进行了更改。到目前为止,在我使用
TBitmap.ScanLine
看到的每个演示中,它们都是从加载图像开始的。 (显然,这正确设置了各种句柄,以至于这种情况最终不会发生?)但是,我并不是在尝试加载图像并对其进行处理;我正在尝试从相机捕获图像数据。如何设置位图,以便可以绘制扫描线而不必先加载图像?
最佳答案
开始绘制之前,应明确设置PixelFormat
。例如,
procedure TForm1.FormPaint(Sender: TObject);
var
bm: TBitmap;
y: Integer;
sl: PRGBQuad;
x: Integer;
begin
bm := TBitmap.Create;
try
bm.SetSize(1024, 1024);
bm.PixelFormat := pf32bit;
for y := 0 to bm.Height - 1 do
begin
sl := bm.ScanLine[y];
for x := 0 to bm.Width - 1 do
begin
sl.rgbBlue := 255 * x div bm.Width;
sl.rgbRed := 255 * y div bm.Height;
sl.rgbGreen := 255 * x div bm.Width;
inc(sl);
end;
end;
Canvas.Draw(0, 0, bm);
finally
bm.Free;
end;
end;
关于delphi - 如何使用扫描线绘制而不先加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33575370/