FMX.FindClosestPixelFormat (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use a FindClosestPixelFormat method. In order to use FindClosestPixelFormat this example builds first a TPixelFormatList to look in. The next code builds a list of pixel formats that have a specified number of bytes per color channel, calls the FindClosestPixelFormat method, and returns the result as a string.

Code

public
    { Public declarations }
    function TestFindClosestPixelFormat(ABitmap: TBitmap;
      PixelFormat: TPixelFormat; NumberOfBytes: integer): string;
procedure TForm1.Button1Click(Sender: TObject);
var
  Img: TBitmap;
  ImgFormat: String;
  pf: TPixelFormat;
begin
  Img := TBitmap.Create;
  try
    Img.LoadFromFile('Photo.jpg');
  finally
    pf := img.Image.PixelFormat;
    ImgFormat := TestFindClosestPixelFormat(Img, pf, 8);
    Showmessage(ImgFormat);
  end;
  img.Free;
end;
function TForm1.TestFindClosestPixelFormat(ABitmap: TBitmap;
  PixelFormat: TPixelFormat; NumberOfBytes: integer): string;
var
  FormatsList: TPixelFormatList;
  pf: TPixelFormat;
begin
  FormatsList := TPixelFormatList.Create;
  try
    for pf := Low(TPixelFormat) to High(TPixelFormat) do
      if PixelFormatBytes[pf] = NumberOfBytes then
        FormatsList.Add(pf);

    pf := FindClosestPixelFormat(ABitmap.Image.PixelFormat, FormatsList);
    Result := PixelFormatToString(pf);
  finally
    FormatsList.Free;
  end;
end;

Uses

See Also