How to draw part of the image?

I have a regular bitmap uploaded by PNGImage. The following code shows the whole image; but I am looking to show, for example, an example below. I want to basically reduce the virtual "place" where it will be painted. Please note that I cannot just resize the PaintBox for reasons that I can list if someone asks. I assume that I need to use Rects and / or the Copy function, but I could not figure it out myself. Does anyone know how to do this?

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

+4
source share
1 answer

One way is to change the clipping area of ​​your canvas for drawing:

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


Of course, I'm sure that you know that (0, 0 in your Canvas.Draw call are coordinates. You can draw at any time convenient for you:

 ... 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 crop the Paintbox area and don’t want to change the original bitmap (FBitmap) and don’t want to create a temporary copy, you can directly call AlphaBlend instead of 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); 
+5
source

Source: https://habr.com/ru/post/1201456/


All Articles