简体   繁体   English

Delphi XE4 FireMonkey / iOS DrawBitmap无法正常工作

[英]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. 因此,通过我的例程,我为TImage内显示的TBitmap( ABitmap )计算了新的宽度/高度。

Here's the code that resize and draw ABitmap 这是调整和绘制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. 我所得到的是一个完全空白的区域,其中显示了TImage。

Have a look at TCanvas.BeginScene(..) : Before you paint something onto the canvas, you have to begin the scene, and end it later on: 看看TCanvas.BeginScene(..) :在画布上绘画之前,必须先开始场景,然后在稍后结束

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() . 就是这样:您只是缺少对BeginScene()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) . 只需使用替代构造函数TBitmap.CreateFromFile(const AFileName: string)

  2. You also need to make sure that FBitmapBuffer points to a valid TBitmap object. 您还需要确保FBitmapBuffer指向有效的TBitmap对象。 You should insert a check like if not Assigned(FBitmapBuffer) then FBitmapBuffer := TBitmap.Create(..); 您应该插入检查,例如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. 此外,您每次都会创建一个新的TBitmap对象,并且在完成工作后不会释放它。 Everytime you call your procedure, your memory gets filled up with TBitmaps you're never going to see again. 每次调用过程时,您的内存都会被TBitmaps充满,您将再也看不到它。 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? 另外,您确定要例如将新位图的宽度设置为Round(TmpNW)吗? I guess you were meant to type Trunc(TmpNW) instead. 我想您应该输入Trunc(TmpNW)代替。

  5. Why are you using the bitmap of your firemonkey component to load the image from disk and not your FBitmapBuffer object? 为什么要使用firemonkey组件的位图从磁盘而不是FBitmapBuffer对象加载图像?

  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. 最后:尽管我们现在已经可以将位图粘贴到TImage上了,但老实说,我不知道您最初打算做什么。 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. 一个TiImageWrapMode ,让你设置它是如何的图像将被缩放的财产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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