PaintPalette example for Delphi

This example sets the paint palette to the palette of each Lead control. It then loads two images and dithers them to 8 bits per pixel with optimized palettes, and displays them side by side. In 8-bit display mode, you can see the palette problems that result when you do not use a fixed palette.

procedure TForm1.Button3Click(Sender: TObject);

var
HeightFactor1, WidthFactor1, HeightFactor2, WidthFactor2, HeightAllowed, WidthAllowed: Integer;

begin
  {
  {Let the controls use the bitmaps' own palettes.}
  Lead1.PaintPalette := ppAuto;
  Lead2.PaintPalette := ppAuto;

  {Disable automatic repainting of the image.}
  Lead1.AutoRepaint := False;
  Lead2.AutoRepaint := False;

  {Turn off scroll bars to make sure we use the full client area.}
  Lead1.AutoScroll := False;
  Lead2.AutoScroll := False;

  {Clear the bitmaps from memory}
  Lead1.Bitmap := 0;
  Lead2.Bitmap := 0;
  Screen.Cursor := crHourglass; {Hourglass}

  {Load the bitmaps. These hard-coded path names may be different on your system.}
  Lead1.Load('C:\lead\IMAGES\IMAGE1.CMP', 0, 0, 1);
  Lead2.Load('C:\lead\IMAGES\IMAGE2.CMP', 0, 0, 1);

  {Change the bitmaps to 8 BPS with optimized palettes}
  Lead1.ColorRes(8, CRF_OPTIMIZEDPALETTE, CRF_FLOYDSTEINDITHERING, 0);
  Lead2.ColorRes(8, CRF_OPTIMIZEDPALETTE, CRF_FLOYDSTEINDITHERING, 0);

  {Make the controls visible so that we can size and position them.}
  {They will not really appear until we load an image and paint it.}
  Lead1.Visible := True;
  Lead2.Visible := True;

  {Set the variables used for preserving the aspect ratio.}
  HeightFactor1 := Lead1.BitmapHeight;
  WidthFactor1 := Lead1.BitmapWidth;
  HeightFactor2 := Lead2.BitmapHeight;
  WidthFactor2 := Lead2.BitmapWidth;
  HeightAllowed := (ClientHeight * 8) div 10;
  WidthAllowed := (ClientWidth * 45) div 100;

  {Center each LEAD control on half of the form, preserving the aspect ratio.}
  {Check to see if using the maximum width will make the image too tall.}
  {Set the dimensions based on the result.}
  If (WidthAllowed * HeightFactor1) div WidthFactor1 < HeightAllowed Then
  begin
    Lead1.Left :=  round((ClientWidth / 4) - (WidthAllowed / 2));
    Lead1.Width := round(WidthAllowed);
    Lead1.Height := round((Lead1.Width * HeightFactor1) / WidthFactor1);
    Lead1.Top := round((ClientHeight - Lead1.Height) / 2);
  end
  Else
  begin
    Lead1.Top := round((ClientHeight - HeightAllowed) / 2);
    Lead1.Height := HeightAllowed;
    Lead1.Width := round((Lead1.Height * WidthFactor1) / HeightFactor1);
    Lead1.Left := round((ClientWidth / 4) - (Lead1.Width / 2));
  End;

  If (WidthAllowed * HeightFactor2) div WidthFactor2 < HeightAllowed Then
  begin
    Lead2.Left := round((ClientWidth * 3/4) - (WidthAllowed / 2));
    Lead2.Width := WidthAllowed;
    Lead2.Height := round((Lead2.Width * HeightFactor2) / WidthFactor2);
    Lead2.Top := round((ClientHeight - Lead2.Height) / 2);
  end
  Else
  begin
    Lead2.Top := round((ClientHeight - HeightAllowed) / 2);
    Lead2.Height := HeightAllowed;
    Lead2.Width := round((Lead2.Height * WidthFactor2) / HeightFactor2);
    Lead2.Left := round((ClientWidth * 3 / 4) - (Lead2.Width / 2));
  End;

  {Set the image display sizes to match the LEAD controls}
   Lead1.SetDstRect(0, 0, Lead1.BitmapWidth, Lead1.BitmapHeight);
   Lead1.SetDstClipRect(0, 0, Lead1.BitmapWidth, Lead1.BitmapHeight);
   Lead2.SetDstRect(0, 0, Lead2.BitmapWidth, Lead2.BitmapHeight);
   Lead2.SetDstClipRect(0, 0, Lead2.BitmapWidth, Lead2.BitmapHeight);

  {Display the images}
  Lead1.ForceRepaint;
  Lead2.ForceRepaint;
  Screen.Cursor := crDefault;

end;