我的带有 TGraphic.Draw(Canvas, Rect) 的代码不起作用
My code with TGraphic.Draw(Canvas, Rect) does not work
在 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;
我怀疑错误出在位图上的绘图中canvas?
此处正确的绘制方法是在目标位图的 canvas 上调用 Draw
,传递源图形。您调用的方法在 TGraphic
中声明为 protected
,这表明您不打算从消费者代码中调用它。
所以
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;
在 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;
我怀疑错误出在位图上的绘图中canvas?
此处正确的绘制方法是在目标位图的 canvas 上调用 Draw
,传递源图形。您调用的方法在 TGraphic
中声明为 protected
,这表明您不打算从消费者代码中调用它。
所以
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;