Recent

Author Topic: Does TPaintBox work with other than *.BMP?  (Read 353 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 197
Does TPaintBox work with other than *.BMP?
« on: February 25, 2025, 11:34:35 pm »
When I import a BMP file into a TPaintBox, it works fine.  But If I import a .JPG or PNG, it balks.  I get a runtime error says File Format is bad.  Is there something I need to do to be able to import JPG's and PNG's (or other image formats)?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Does TPaintBox work with other than *.BMP?
« Reply #1 on: February 26, 2025, 12:16:41 am »
If you need access to any registered image file format use a TPicture rather than a TBitmap. It can automatically detect the file format and then read the image into uncompressed memory. From there you can access the image via the generic property Graphic or, reformatted, via Bitmap, PNG, JPEG, or ICO properties (https://wiki.freepascal.org/Developing_with_Graphics#Working_with_TBitmap_and_other_TGraphic_descendents):
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     PaintBox1: TPaintBox;
  4.     procedure FormCreate(Sender: TObject);
  5.     procedure FormDestroy(Sender: TObject);
  6.     procedure PaintBox1Paint(Sender: TObject);
  7.   private
  8.     pic: TPicture;
  9.   end;
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. begin
  13.   pic := TPicture.Create;
  14.   pic.LoadFromFile('project1.ico');  // or any other registered graphic file format such as .jpg, .png, .bmp, ...
  15. end;
  16.  
  17. procedure TForm1.FormDestroy(Sender: TObject);
  18. begin
  19.   pic.Free;
  20. end;
  21.  
  22. procedure TForm1.PaintBox1Paint(Sender: TObject);
  23. begin
  24.   Paintbox1.Canvas.Draw(10, 10, pic.Graphic);
  25. end;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1501
    • Lebeau Software
Re: Does TPaintBox work with other than *.BMP?
« Reply #2 on: February 26, 2025, 07:01:56 pm »
When I import a BMP file into a TPaintBox, it works fine.  But If I import a .JPG or PNG, it balks.  I get a runtime error says File Format is bad.  Is there something I need to do to be able to import JPG's and PNG's (or other image formats)?

TPaintBox is just a Canvas that you draw on. It has no concept of BMPs or other images. What are you importing, exactly? Can you show the code you are having trouble with?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Boleeman

  • Hero Member
  • *****
  • Posts: 885
Re: Does TPaintBox work with other than *.BMP?
« Reply #3 on: February 27, 2025, 09:26:11 am »
Use a TbgraBmp in conjunction with a TPaintbox to load jpg.
Also if the quality of the jpg is poor then expect the loaded picture to be pixelated/poor.

Loading png to a TPaintbox should work OK.
Below is a working example:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, ExtDlgs, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnLoadPng: TButton;
  16.     OpenDialog1: TOpenDialog;
  17.     PaintBox1: TPaintBox;
  18.     procedure btnLoadPngClick(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure PaintBox1Paint(Sender: TObject);
  21.   private
  22.        FBitmap: TBitmap;
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36.  
  37. procedure TForm1.btnLoadPngClick(Sender: TObject);
  38. var
  39.   Png: TPortableNetworkGraphic;
  40.  
  41. begin
  42.   if OpenDialog1.Execute then
  43.   begin
  44.     FBitmap := TBitmap.Create;
  45.     Png := TPortableNetworkGraphic.Create;
  46.     try
  47.       Png.LoadFromFile(OpenDialog1.FileName);
  48.       FBitmap.SetSize(Png.Width, Png.Height);
  49.       FBitmap.Canvas.Draw(0, 0, Png);
  50.       PaintBox1.Width := Png.Width;
  51.       PaintBox1.Height := Png.Height;
  52.       PaintBox1.Invalidate;
  53.     finally
  54.       Png.Free;
  55.     end;
  56.   end;
  57. end;
  58.  
  59. procedure TForm1.PaintBox1Paint(Sender: TObject);
  60. begin
  61.  if (FBitmap <> nil) and (FBitmap.Width > 0) and (FBitmap.Height > 0) then
  62.     PaintBox1.Canvas.Draw(0, 0, FBitmap);
  63. end;
  64.  
  65. end.
« Last Edit: February 27, 2025, 09:33:29 am by Boleeman »

 

TinyPortal © 2005-2018