How to use CopyRect method in Delphi

I load an image from disk and want to copy (part of it) to the second TImage file:

Image1.Picture.LoadFromFile(S); with Image1.Picture.Bitmap do Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect); 

Image2 just shows a white rectangle, while Image1 does not show a file from disk. If I delete the second statement, Image1 will show the image. (The strangest thing: if I only comment on the CopyRect instruction and leave the string ā€œcā€ (an empty statement), Image1 does not show a single one!)

How to use CopyRect to copy parts of an image?

change
When I separate two operators into two separate actions (buttons), the following happens:

  • Uploading Images and Impressions to Image1
  • Image1 disappears (!), And Image2 shows a white rectangle.

By the way, I'm using Delphi 2009.

+6
source share
1 answer

TCanvas.CopyRect copies the rectangle using StretchBlt . StretchBlt requires a bitmap. If you load any other graphic type into your image, its Picture.Bitmap empty. In fact, a bitmap is only created when you reference it: with Image1.Picture.Bitmap do .

You can use a temporary raster map for a reason:

 var Bmp: TBitmap; begin Image1.Picture.LoadFromFile(S); Bmp := TBitmap.Create; try Bmp.Assign(Image1.Picture.Graphic); with Bmp do Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect); finally Bmp.Free; .. 
+6
source

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


All Articles