【问题标题】:How do I draw with scanlines without loading an image first?如何在不先加载图像的情况下使用扫描线进行绘制?
【发布时间】:2015-11-06 20:40:47
【问题描述】:

我正在尝试执行以下操作:

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?
  • @AndreasRejbrand:不。那行得通。将其发布为答案,我会接受。 :)

标签: delphi delphi-xe tbitmap


【解决方案1】:

您应该在开始绘制之前明确设置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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 2022-01-20
    • 2017-10-01
    • 1970-01-01
    • 2013-01-13
    • 2022-08-16
    相关资源
    最近更新 更多