2014-02-18 97 views
4

我有一个普通的位图加载一个PNGImage。以下代码显示整个图像;但我正在寻找的是例如下面的示例。我基本上想要减少它将被绘制的虚拟“地点”。请注意,我不能仅仅因为我可以枚举的原因调整PaintBox的大小,如果有人问。我想我必须使用Rects和一些复制功能,但我无法自己弄清楚。有谁知道该怎么办?如何绘制图像的一部分?

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; 

enter image description here

+3

看到'TCanvas.CopyRect' –

回答

5

一种方法是修改你的颜料盒的画布的剪辑区域:

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


当然,我敢肯定,你知道,(0, 0Canvas.Draw呼叫是坐标。您可以绘制等。无论您喜欢:

... 
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); 
+0

随着'CopyRect'。 Preciselly。谢谢! – Guill