首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GrayScale中绘制来自TPngImageList的位图图像

在GrayScale中绘制来自TPngImageList的位图图像
EN

Stack Overflow用户
提问于 2018-06-07 23:59:19
回答 1查看 208关注 0票数 1

使用False作为最后一个参数,TImageList允许您将其图像之一绘制为处于禁用状态的位图。

代码语言:javascript
复制
ImageList.Draw(DestBitmap.Canvas, 0, 0, ImageIndex, False);

我想这样做,同时也要GrayScale目标位图。

我有以下代码:

代码语言:javascript
复制
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中)

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 00:12:51

您也可以对TPngImageList使用Draw方法。要获得灰度图像,必须首先使用pngGrayscaleOnDisabled选项在其PngOptions属性中启用该选项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50745639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档