How to access TBitmap pixels directly in FMX2 (TBitmap.ScanLine replacement)?

三世轮回 提交于 2020-12-30 19:36:22

问题


The FMX.Types.TBitmap class has the ScanLine property in FMX (FireMonkey), but it seems this property was removed, and is missing in FMX2 (FireMonkey FM2).

Is there any workaround ? How do we supposed to access TBitmap content directly in FMX2 ?


回答1:


For direct access you are expect to use the Map method. The documentation includes a number of examples, such as FMX.AlphaColorToScanline:

function TForm1.TestAlphaColorToScanline(ABitmap: TBitmap;
  start, count: integer): TBitmap;
var
  bitdata1, bitdata2: TBitmapData;
begin
  Result := TBitmap.Create(Round(ABitmap.Width), Round(count));
  if (ABitmap.Map(TMapAccess.maRead, bitdata1) and
    Result.Map(TMapAccess.maWrite, bitdata2)) then
  begin
    try
      AlphaColorToScanline(@PAlphaColorArray(bitdata1.Data)
        [start * (bitdata1.Pitch div GetPixelFormatBytes(ABitmap.PixelFormat))],
        bitdata2.Data, Round(Result.Height * Result.Width),
        ABitmap.PixelFormat);
    finally
      ABitmap.Unmap(bitdata1);
      Result.Unmap(bitdata2);
    end;
  end;
end;



回答2:


Here is an example for C++Builder (the current docs are completely missing such):

int X, Y;
TBitmapData bm;

// get bitmap data access !
if ( Image1->Bitmap->Map(TMapAccess::maReadWrite, bm) )
{
    unsigned int* data = (unsigned int*)bm.Data;

    // i.e. clear data with alpha color
    memset(data, 0,
    Image1->Width * Image1->Height * sizeof(unsigned int));

    // test direct pixel access here
    for (X = 20; X <= 200; X++)
    {
        for (Y = 10; Y <= 100; Y++)
        {
            //MyBitmap->Pixels[X][Y] = claLime;  // does not work anymore !
            bm.SetPixel(X, Y, claLime);
        }
    }

    // now write back the result !
    Image1->Bitmap->Unmap(bm);
}
else
{
    MessageDlg("Could not map the image data for direct access.",
    TMsgDlgType::mtWarning, TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
}


来源:https://stackoverflow.com/questions/15185502/how-to-access-tbitmap-pixels-directly-in-fmx2-tbitmap-scanline-replacement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!