I've put together this code which creates a grayscale Bitmap from a control:
procedure TForm1.PseudoDisableControl(const AWinControl: TWinControl; const AImage: TImage);
var
Bmp: TBitmap;
function Control2Bitmap(Control_: TWinControl): TBitmap;
// http://delphidabbler.com/tips/24
begin
Result := TBitmap.Create;
with Result do
begin
Height := Control_.Height;
Width := Control_.Width;
Canvas.Handle := CreateDC(nil, nil, nil, nil);
Canvas.Lock;
Control_.PaintTo(Canvas.Handle, 0, 0);
Canvas.Unlock;
DeleteDC(Canvas.Handle);
end;
end;
type
PRGB32Array = ^TRGB32Array;
TRGB32Array = packed array [0 .. MaxInt div SizeOf(TRGBQuad) - 1] of TRGBQuad;
procedure MakeGrey(Bitmap: TBitmap);
// http://stackoverflow.com/questions/4101855/converting-a-pngimage-to-grayscale-using-delphi
var
w, h: integer;
y: integer;
sl: PRGB32Array;
x: integer;
grey: byte;
begin
Bitmap.PixelFormat := pf32bit;
w := Bitmap.Width;
h := Bitmap.Height;
for y := 0 to h - 1 do
begin
sl := Bitmap.ScanLine[y];
for x := 0 to w - 1 do
with sl[x] do
begin
grey := (rgbBlue + rgbGreen + rgbRed) div 3;
rgbBlue := grey;
rgbGreen := grey;
rgbRed := grey;
end;
end;
end;
begin
Bmp := Control2Bitmap(AWinControl);
try
MakeGrey(Bmp);
AImage.AutoSize := True;
AImage.Picture.Bitmap := Bmp;
finally
Bmp.Free;
end;
end;
This creates this result, for example:
However, I need to make the image look like a disabled control:
How could I achieve this?
EDIT: MBo's solution works great for me with a factor of 3. However, with a trackbar control (TAdvTrackbar
from TMS), a light area around the trackbar is left: