DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

AdjustImage (TCanvas overload)

Adjusts the area defined by Rect of the image displayed on SrcCvs, storing the resulting image on DestCvs. Any one or more of the brightness, contrast, tint, saturation and gamma values of the image can be adjusted. The image may also be inverted (negated). Returns True if the adjustment succeeds, False if it fails.

function AdjustImage(const DestCvs, SrcCvs: Graphics.TCanvas;
  const Rect: Types.TRect; const Brightness: TAIBrightness = AINulBrightness;
  const Contrast: TAIContrast = AINulContrast; const Tint: TAITint = AINulTint;
  const Saturation: TAISaturation = AINulSaturation;
  const Gamma: TAIGamma = AINulGamma; const Invert: Boolean = False): Boolean;
  overload;
var
  CA: Windows.TColorAdjustment; // colour adjustment info for API call
  OldMode: Integer;             // saves stretch blt mode for later restoration
begin
  OldMode := Windows.SetStretchBltMode(DestCvs.Handle, Windows.HALFTONE);
  try
    Windows.GetColorAdjustment(DestCvs.Handle, CA);
    with CA do
    begin
      caBrightness := Brightness;     // -100..100
      caContrast := Contrast;         // -100..100
      caRedGreenTint := Tint;         // -100..100
      caColorfulness := Saturation;   // -100..100
      caRedGamma := Gamma;            // 2500..65000
      caGreenGamma := Gamma;          // 2500..65000
      caBlueGamma := Gamma;           // 2500..65000
      if Invert then
        caFlags := caFlags or Windows.CA_NEGATIVE
      else
        caFlags := caFlags and not Windows.CA_NEGATIVE;
    end;
    Result:= Windows.SetColorAdjustment(DestCvs.Handle, CA);
    DestCvs.CopyRect(Rect, SrcCvs, Rect);
  finally
    Windows.SetStretchBltMode(DestCvs.Handle, OldMode);
  end;
end;

AdjustImage (TBitmap overload 1)

Adjusts the image contained in bitmap SrcBmp and stores the resulting image in bitmap DestBmp. Any one or more of the brightness, contrast, tint, saturation and gamma values of the bitmap's image can be adjusted. The image may also be inverted (negated). Returns True if the adjustment succeeds, False if it fails.

function AdjustImage(const DestBmp, SrcBmp: Graphics.TBitmap;
  const Brightness: TAIBrightness = AINulBrightness;
  const Contrast: TAIContrast = AINulContrast; const Tint: TAITint = AINulTint;
  const Saturation: TAISaturation = AINulSaturation;
  const Gamma: TAIGamma = AINulGamma; const Invert: Boolean = False): Boolean;
  overload;
begin
  DestBmp.Width := SrcBmp.Width;
  DestBmp.Height := SrcBmp.Height;
  Result := AdjustImage(
    DestBmp.Canvas, SrcBmp.Canvas, SrcBmp.Canvas.ClipRect,
    Brightness, Contrast, Tint, Saturation, Gamma, Invert
  );
end;

AdjustImage (TBitmap overload 2)

Adjusts the image contained in the given bitmap, updating it in place. Any one or more of the brightness, contrast, tint, saturation and gamma values of the bitmap's image can be adjusted. The image may also be inverted (negated). Returns True if the adjustment succeeds, False if it fails.

function AdjustImage(const Bmp: Graphics.TBitmap;
  const Brightness: TAIBrightness = AINulBrightness;
  const Contrast: TAIContrast = AINulContrast; const Tint: TAITint = AINulTint;
  const Saturation: TAISaturation = AINulSaturation;
  const Gamma: TAIGamma = AINulGamma; const Invert: Boolean = False): Boolean;
  overload;
begin
  Result := AdjustImage(
    Bmp.Canvas, Bmp.Canvas, Bmp.Canvas.ClipRect,
    Brightness, Contrast, Tint, Saturation, Gamma, Invert
  );
end;

View the whole database.

Go to the DelphiDabbler website.