且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绘制图像的一部分?

更新时间:2022-12-12 08:04:06

一种方法是修改paintbox画布的剪裁区域:

One way is to modify the clipping region of your paintbox's canvas:

...
IntersectClipRect(PaintBox1.Canvas.Handle, 20, 20,
    PaintBox1.Width - 20, PaintBox1.Height - 20);
PaintBox1.Canvas.Draw(0, 0, FBitmap, FOpacity);


当然,我确定你知道 Canvas.Draw 调用中的c>(0,0 )是坐标,您可以根据自己的喜好绘制:


Of course, I'm sure you're aware that the (0, 0 in your Canvas.Draw call are coordinates. You can draw to whereever you like:

...
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.Draw

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);