简体   繁体   English

如何直接在FMX2中访问TBitmap像素(TBitmap.ScanLine替换)?

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

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). FMX.Types.TBitmap类在FMX(FireMonkey)中具有ScanLine属性,但似乎此属性已被删除,并且在FMX2(FireMonkey FM2)中丢失。

Is there any workaround ? 有没有解决方法? How do we supposed to access TBitmap content directly in FMX2 ? 我们如何直接在FMX2中访问TBitmap内容?

For direct access you are expect to use the Map method. 对于直接访问,您需要使用Map方法。 The documentation includes a number of examples, such as FMX.AlphaColorToScanline : 该文档包含许多示例,例如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;

Here is an example for C++Builder (the current docs are completely missing such): 这是C ++ Builder的一个示例(当前的文档完全没有这样):

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM