1

I have followed this answer to crop image: how do I crop a bitmap “in place”?

In delphi 7 I have a TImage Image_center.

Image := TPngObject.Create;
try
  Image.LoadFromStream(Stream);
  Image_center.Picture.Graphic := Image;
  Image_center.width := Image.width;
  Image_center.height := Image.height;
  Image_center.Left := ( form1.clientWidth div 2 ) - (Image_center.width div 2);
  CropBitmap(Image_center.Picture.Bitmap, 1, 45,   Image.width, Image.height-45);
finally
  Image.Free;
end;

But the result is that the TImage contains white bitmap. If I skip/comment out the CropBitmap function, so I can see the image. So there is not problem with loading. Why I see the white area instead of the image?

procedure CropBitmap(InBitmap : TBitmap; X, Y, W, H :Integer);
begin
  BitBlt(InBitmap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X, Y, SRCCOPY);
  InBitmap.Width :=W;
  InBitmap.Height:=H;
end;

Delphi 7 Bitmap methods:

In TBitmap

~TBitmap
            Assign
            Create
            Destroy
            Dormant
            FreeImage
            HandleAllocated

LoadFromClipboardFormat
            LoadFromResourceID
            LoadFromResourceName
            LoadFromStream
            Mask
            ReleaseHandle

ReleaseMaskHandle
            ReleasePalette
            SaveToClipboardFormat
            SaveToStream
            TBitmap

Derived from TGraphic

LoadFromFile
            SaveToFile

Derived from TInterfacedPersistent

AfterConstruction
            QueryInterface

Derived from TPersistent

GetNamePath

Derived from TObject

BeforeDestruction
            ClassInfo
            ClassName
            ClassNameIs
            ClassParent
            ClassType
            CleanupInstance

DefaultHandler
            Dispatch
            FieldAddress
            Free
            FreeInstance
            GetInterface
            GetInterfaceEntry

GetInterfaceTable
            InheritsFrom
            InitInstance
            InstanceSize
            MethodAddress
            MethodName
            NewInstance

SafeCallException
John Boe
  • 3,501
  • 10
  • 37
  • 71
  • 1
    It is because the `Bitmap` property of `Image_center` is empty all the time! You assign it `TPngObject` and that is not magically converted to a bitmap. When you refer to `Image_center.Bitmap` you are inadvertently also deleting the `.png` image. As I said in my comment to your previous question, load the image into a `TBitmap` first (can be `Image_center.Bitmap`, then (overpaint or) `BitBlt` directly into the `Image_center.Bitmap` – Tom Brunberg Nov 25 '18 at 18:32
  • It's not clear to me how to load the PNG image to the TImage bitmap. I think the Vcl.Graphics.TPicture.LoadFromStream cannot understand the png type because I am using D7 – John Boe Nov 25 '18 at 18:43
  • Image.AssignTo(Image_center.Picture.Bitmap) – Sertac Akyuz Nov 25 '18 at 18:53
  • Thanks but I don't have this function `AssignTo` in Delphi 7. – John Boe Nov 25 '18 at 18:57
  • `Image_center.Picture.Bitmap.Canvas.Brush.Color := ???;` `Image_center.Picture.Bitmap.SetSize(Image.Width, Image.Height);` `Image.Draw(Image_center.Picture.Bitmap.Canvas, Rect(0, 0, Image.Width, Image.Height));` – Sertac Akyuz Nov 25 '18 at 19:05
  • Not such function SetSize. See question notes, edit – John Boe Nov 25 '18 at 19:24
  • Set the widht and height. – Sertac Akyuz Nov 25 '18 at 19:27
  • Thank you! Now it copied. Cropped successfuly. – John Boe Nov 25 '18 at 19:45

1 Answers1

3

You (falsely) assume that Image_center.Picture.Bitmap will give you the picture as bitmap. That is only true if it is a bitmap, otherwise it will overwrite your graphic with an empty bitmap.

Instead, you can draw your PNG image on the bitmap, something like:

with Image_center.Picture.Bitmap do
begin
  Width := Image.Width;
  Height := Image.Height;
  Canvas.Draw(0,0, Image);
end;

After that you can use the bitmap in Image_center.Picture.Graphic or Image_center.Picture.Bitmap to do whatever you want.

Keep in mind though, that this way you will lose any transparency you had in the PNG image, and it's not trivial to get that back, although you may be able to if you read How to get bitmap transparancy without the need to paint first?.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210