使用False作为最后一个参数,TImageList允许您将其图像之一绘制为处于禁用状态的位图。
ImageList.Draw(DestBitmap.Canvas, 0, 0, ImageIndex, False);我想这样做,同时也要GrayScale目标位图。
我有以下代码:
procedure ConvertBitmapToGrayscale(const Bitmap: TBitmap);
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
P: PPixelRec;
Gray: Byte;
begin
Assert(Bitmap.PixelFormat = pf32Bit);
for Y := 0 to (Bitmap.Height - 1) do
begin
P := Bitmap.ScanLine[Y];
for X := 0 to (Bitmap.Width - 1) do
begin
Gray := Round(0.30 * P.R + 0.59 * P.G + 0.11 * P.B);
P.R := Gray;
P.G := Gray;
P.B := Gray;
Inc(P);
end;
end;
end;
procedure DrawIconShadowPng(ACanvas: TCanvas; const ARect: TRect; ImageList:
TCustomImageList; ImageIndex: Integer);
var
ImageWidth, ImageHeight: Integer;
GrayBitMap : TBitmap;
begin
ImageWidth := ARect.Right - ARect.Left;
ImageHeight := ARect.Bottom - ARect.Top;
with ImageList do
begin
if Width < ImageWidth then ImageWidth := Width;
if Height < ImageHeight then ImageHeight := Height;
end;
GrayBitMap := TBitmap.Create;
try
GrayBitmap.PixelFormat := pf32bit;
GrayBitMap.SetSize(ImageWidth, ImageHeight);
ImageList.Draw(GrayBitMap.Canvas, 0, 0, ImageIndex, False);
ConvertBitmapToGrayscale(GrayBitMap);
BitBlt(ACanvas.Handle, ARect.Left, ARect.Top, ImageWidth, ImageHeight,
GrayBitMap.Canvas.Handle, 0, 0, SRCCOPY);
finally
GrayBitMap.Free;
end;
end;这样做的问题是生成的图像具有白色背景。

怎样才能让背景变得透明?
我使用的是TPngImageList,因为它比普通的TImageList更好地处理Png图像。(在XE4中)
发布于 2018-06-08 00:12:51
您也可以对TPngImageList使用Draw方法。要获得灰度图像,必须首先使用pngGrayscaleOnDisabled选项在其PngOptions属性中启用该选项。
https://stackoverflow.com/questions/50745639
复制相似问题