我有一个普通的Bitmap加载PNG Image.以下代码显示整个图像;但我要找的是像下面的示例一样显示.我基本上想要减少它将被绘制的虚拟“位置”.请注意,我不能仅仅因为有人问我可以枚举的
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
PaintBox1.Canvas.Brush.Color := clBlack;
PaintBox1.Brush.Style := bsSolid;
PaintBox1.Canvas.FillRect(GameWindow.Screen.ClientRect);
PaintBox1.Canvas.Draw(0, 0, FBitmap, FOpacity);
end;
一种方法是修改paintbox画布的剪裁区域:
...
IntersectClipRect(PaintBox1.Canvas.Handle, 20, 20,
PaintBox1.Width - 20, PaintBox1.Height - 20);
PaintBox1.Canvas.Draw(0, 0, FBitmap, FOpacity);
当然,我确定你知道你的Canvas.Draw调用中的0,0是坐标.你可以画到你喜欢的地方:
...
FBitmap.Canvas.CopyRect(Rect(0, 0, 80, 80), FBitmap.Canvas,
Rect(20, 20, 100, 100));
FBitmap.SetSize(80, 80);
PaintBox1.Canvas.Draw(20, 20, FBitmap, FOpacity);
如果您不想剪切绘图框的区域,并且不想修改源位图(FBitmap),并且不想对其进行临时复制,则可以直接调用AlphaBlend而不是通过Canvas.画:
var
BlendFn: TBlendFunction;
begin
BlendFn.BlendOp := AC_SRC_OVER;
BlendFn.BlendFlags := 0;
BlendFn.SourceConstantAlpha := FOpacity;
BlendFn.AlphaFormat := AC_SRC_ALPHA;
winapi.windows.AlphaBlend(PaintBox1.Canvas.Handle,
20, 20, PaintBox1.Width - 20, PaintBox1.Height - 20,
FBitmap.Canvas.Handle, 20, 20, PaintBox1.Width - 20, PaintBox1.Height - 20,
BlendFn);
