-3

在 Delphi 10 Seattle 中,我需要将图像插入到 ImageList 中。该图像位于 TGraphicControl 的后代中(请参见下面的源代码)。插入似乎有效。但是,我在 ImageList 中只得到一个白色矩形:

function InsertCloudImageIntoImageList(AdvCloudImage1: TAdvCloudImage): Integer;
// TAdvCloudImage = class(TGraphicControl)
// WebPicture is TCloudPicture = class(TGraphic)
var
  TempBitmap: TBitmap;
  R: TRect;
begin
  Result := 0;
  TempBitmap := TBitmap.Create;
  try
    TempBitmap.SetSize(16, 16);
    R.Width  := 16;
    R.Height := 16;
    R.Top := 0;
    R.Left := 0;

    AdvCloudImage1.WebPicture.Draw(TempBitmap.Canvas, R);
    Result := Form1.ImageList1.Add(TempBitmap, nil);
  finally
    TempBitmap.Free;
  end;
end;

我怀疑错误出现在位图画布上的绘图中?

4

1 回答 1

1

此处绘制的正确方法是调用Draw目标位图的画布,传递源图形。您调用的方法是在其中声明protectedTGraphic,表明您不打算从消费者代码中调用它。

所以而不是

AdvCloudImage1.WebPicture.Draw(TempBitmap.Canvas, R);

你应该使用

TempBitmap.Canvas.Draw(0, 0, AdvCloudImage1.WebPicture);

这大大简化了函数,因为您不再需要该TRect变量。Result此外,多次分配是没有意义的。整个函数可以是:

function InsertCloudImageIntoImageList(AdvCloudImage1: TAdvCloudImage): Integer;
var
  TempBitmap: TBitmap;
begin
  TempBitmap := TBitmap.Create;
  try
    TempBitmap.SetSize(16, 16);
    TempBitmap.Canvas.Draw(0, 0, AdvCloudImage1.WebPicture);
    Result := Form1.ImageList1.Add(TempBitmap, nil);
  finally
    TempBitmap.Free;
  end;
end;
于 2016-01-04T20:16:52.380 回答