Question

I have an ordinary Bitmap loaded with a PNGImage. The following code shows the whole image; but what I am looking for is to show like the sample below for example. I want basically to reduce the virtual "place" where it would be painted. Note that I can't just resize the PaintBox by reasons I can enumerate if someone asks. I guess I have to use Rects and or some Copy function, but I could not figure out by myself. Does anyone know how to do?

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

Was it helpful?

Solution

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


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


If you don't want to clip the paintbox's region, and don't wan to modify your source bitmap (FBitmap), and don't want to make a temporary copy of it, you can then directly call AlphaBlend instead of through 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top