简体   繁体   中英

Delphi XE4 FireMonkey/iOS DrawBitmap not working

So through my routine I have calculated new width/height I want for my TBitmap ( ABitmap ) shown inside a TImage.

Here's the code that resize and draw ABitmap

var
  TmpScale: Double;
  TmpNH: Single;
  TmpNW: Single;
  P: string;
begin      
  P := FAppDataDirPath + 'Library' + PathDelim + 'assets' + PathDelim + 'app_header.png';
  if FileExists(P) then
    begin
      ImageLogo.Bitmap := TBitmap.Create(1,1);
      ImageLogo.Bitmap.LoadFromFile(P);

      TmpScale := ImageLogo.Width / ImageLogo.Bitmap.Width;
      TmpNW := ImageLogo.Bitmap.Width * TmpScale;
      TmpNH := ImageLogo.Bitmap.Height * TmpScale;

      FBitmapBuffer.Width := Round(TmpNW);
      FBitmapBuffer.Height := Round(TmpNH);
      FBitmapBuffer.Canvas.DrawBitmap(ImageLogo.Bitmap, RectF(0,0,ImageLogo.Bitmap.Width,ImageLogo.Bitmap.Height), RectF(0,0,TmpNW,TmpNH), 255);
      ImageLogo.Bitmap.Assign(FBitmapBuffer);

All I get is a completely blank area where TImage is shown.

Have a look at TCanvas.BeginScene(..) : Before you paint something onto the canvas, you have to begin the scene, and end it later on:

if yourCanvas.BeginScene() then
    try
        yourCanvas.DrawBitmap(..);
    finally
        yourCanvas.EndScene();
    end;

That's pretty much it: You were just missing the call to BeginScene() and EndScene() . But apart from that, I have several suggestions:

Suggestions

  1. You don't have to create a Bitmap with a bogus resolution, just to load stuff from disk. Just use the alternatative constructor TBitmap.CreateFromFile(const AFileName: string) .

  2. You also need to make sure that FBitmapBuffer points to a valid TBitmap object. You should insert a check like if not Assigned(FBitmapBuffer) then FBitmapBuffer := TBitmap.Create(..);

  3. Furthermore, you're creating a new TBitmap object everytime and you're not freeing it after your work is done. Everytime you call your procedure, your memory gets filled up with TBitmaps you're never going to see again. You should free it at the end of your procedure.

  4. Also, are you sure that, for example, setting the width of your new bitmap to Round(TmpNW) is what you want? I guess you were meant to type Trunc(TmpNW) instead.

  5. Why are you using the bitmap of your firemonkey component to load the image from disk and not your FBitmapBuffer object?

  6. Finally: Altough we got the pasting of the bitmap onto the TImage working now, I honestly have no clue what you were trying to do in the first place. Paste the bitmap onto an Image while keeping the aspect ratio? A TiImage has a WrapMode property that let's you set how it's image is going to be scaled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM