Recent

Author Topic: load a PNG file from assets  (Read 3287 times)

Dzandaa

  • Full Member
  • ***
  • Posts: 248
  • From C# to Lazarus
load a PNG file from assets
« on: December 29, 2021, 05:42:54 pm »
Hi everybody,

Trying LAMW on Linux installed yesterday :)

I try to load a .PNG picture that I put in the assets directory of my program.

I just have a button and the Click event is:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.BLoadClick(Sender: TObject);
  2. var
  3.   DeskPict :jBitmap;
  4. begin
  5.   DeskPict.LoadFromAssets('cards.png');
  6. end;
  7.  

' = ' of course

Build... OK.

 "Run" -> "[LAMW] Build Apk and Run".. OK.

The APK run fine until I press the button and then crash...

Any idea with example how to load a .PNG file?

The picture size is 468x250

Thank you.

Dzandaa

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: load a PNG file from assets
« Reply #1 on: December 29, 2021, 06:39:51 pm »

Put a jBitmap component on the form ... then

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.BLoadClick(Sender: TObject);
  2. begin
  3.    Bitmap1.LoadFromAssets('cards.png');
  4. end;
  5.  

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Dzandaa

  • Full Member
  • ***
  • Posts: 248
  • From C# to Lazarus
Re: load a PNG file from assets
« Reply #2 on: December 29, 2021, 08:08:40 pm »
Hi,
Thank you jmpessa.

No crash now, but now, I tried to draw part of my .PNG on a jDrawingView

So I drop a JDrawingView on the Form, Center on Parent with

Height 148 width 116 margins 0
LayoutParamHeight and Width to lpExac

Code: Pascal  [Select][+][-]
  1.  
  2. Pict: jDrawingView;
  3.  
  4. procedure TAndroidModule1.BLoadClick(Sender: TObject);
  5. begin
  6.   DeskPict.LoadFromAssets('cards.png');
  7.   Pict.DrawCroppedBitmap(DeskPict, 11, 11, 0, 0, 36, 50);
  8. // position on Pict: 11, 11, draw DeskPict from x=0, y=0, Width=36, Height=50
  9. end;
  10.  

But I don't know if this is the right thing to do :)
Dzandaa

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: load a PNG file from assets
« Reply #3 on: December 29, 2021, 08:24:16 pm »
Quote
Height 148 width 116

This dont work  on android... use only
LayoutParamHeight and LayoutParamWidth

Quote
Pict: jDrawingView; 

No!!!

Put a jBitmap and a jDrawingView (or jImageView) component on the form ... then

procedure TAndroidModule1.BLoadClick(Sender: TObject);
begin
   Bitmap1.LoadFromAssets('cards.png');
   DrawingView1.DrawCroppedBitmap(Bitmap1.GetImage, ..........);
  //or
   //ImageView1.SetImage(Bitmap1.GetImage);
end;
 

Please, take a look at the "demos"... so you can learn how LAMW works....
« Last Edit: December 29, 2021, 08:26:01 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Dzandaa

  • Full Member
  • ***
  • Posts: 248
  • From C# to Lazarus
Re: load a PNG file from assets
« Reply #4 on: December 30, 2021, 12:08:39 pm »
Hi Again,

Working now, I missed the 'GetImage' function :)

Thank you very much for your help.

Dzandaa

Dzandaa

  • Full Member
  • ***
  • Posts: 248
  • From C# to Lazarus
Re: load a PNG file from assets
« Reply #5 on: January 04, 2022, 02:49:42 pm »
Hi,

I found that when I load a .PNG picture from asset(size width=468 Height=250)

And I use this code:
Code: Pascal  [Select][+][-]
  1.   CardWidth: Integer;
  2.   CardHeight: Integer;
  3.   CardsPict: jBitmap;
  4.  
  5. ...
  6.  CardsPict.LoadFromAssets('cards.png');
  7.  CardWidth := CardsPict.GetWidth();
  8.  CardHeight := CardPict.GetHeight();
  9.  

CardWidth = 250 and CardHeight = 468

Is this normal?

Dzandaa

 

TinyPortal © 2005-2018