Confusion with image formats lazarus / 39-Graphics ---------------------------------------------------------------------- Tomi · 2024-04-03 13:56 Hello! I use a TOpenPictureDialog in my program to open different images. With this dialog box possible to open different image formats, e.g. .BMP, .JPG, .GIF, etc. But what if I want use these image formats in my program, not only the .BMPs? So, I would like open it and then perform various actions on it, e.g. draw on a certain coordinate or copy a part of it onto another image. Now, all of my images are TBitmap in my program, therefore when I try to open a different format (.JPG, .GIF, etc.) with TOpenPictureDialog, I get an error message about wrong image format. This solution is good: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if fileOpener.Execute then   begin     sourceimage:=TBitmap.Create;     sourceimage.LoadFromFile(fileOpener.FileName);and later I can use this sourceimage with drawing and with copyrect() function, for example. But which type do I need to use other image formats? TImage? TGraphic? TPicture? I tried these, but the result is the total confusion in my program, e.g. because I just could draw only TBitmaps with Form1.canvas.draw(). So, can only .BMPs be used or is there a solution to use all supported image formats? For example, transform the opened file type to .BMP within the program? Or can I solely use TBitmap and BMPs? ---------------------------------------------------------------------- lainz · 2024-04-03 14:26 Use BGRABitmap, you can use more than BMP with that... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bgra := TBGRABitmap.Create('file.jpg') ---------------------------------------------------------------------- wp · 2024-04-03 15:58 When you create an image as TBitmap - yes, you can only load bmp files into it. But the LCL has a multi-format class, named TPicture. You can load any format and access the data as its Graphic property: Untested: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  pic: TPicture;...  if FileOpener.Execute then  begin    pic := TPicture.Create;    pic.LoadFromFile(fileOpener.FileName);    // Example: crop the center 100x100 pixels into a bitmap    bmp := TBitmap.Create;    bmp.SetSize(100, 100);    bmp.Canvas.Draw((pic.Width - bmp.Width) div 2, pic.Height - bmp.Height) div 2, pic.Graphic);    bmp.SaveToFile('cropped.bmp');    bmp.Free; ---------------------------------------------------------------------- circular · 2024-04-03 16:03 Indeed, TPicture handles multiple formats. You may need to add FPRead*/FPWrite* to the uses clause. ---------------------------------------------------------------------- cdbc · 2024-04-03 16:03 Hi Have you tried:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if fileOpener.Execute then  Image1.Picture.LoadFromFile(fileOpener.FileName); The 3 answers above are better, but I think this should work too... Regards Benny ---------------------------------------------------------------------- wp · 2024-04-03 16:09 Quote from: circular on April 03, 2024, 06:03:05 pmIndeed, TPicture handles multiple formats. You may need to add FPRead*/FPWrite* to the uses clause. Usually not. Because they are already mentioned in the uses clause of the Graphics unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ...  FPWriteBMP,              // bmp support  FPWritePNG, PNGComn,     // png support  {$IFNDEF DisableLCLPNM}  FPReadPNM, FPWritePNM,   // PNM (Portable aNyMap) support  {$ENDIF}  {$IFNDEF DisableLCLJPEG}  FPReadJpeg, FPWriteJpeg, // jpg support  {$ENDIF}  {$IFNDEF DisableLCLTIFF}  FPReadTiff, FPTiffCmn,   // tiff support  {$ENDIF}  {$IFNDEF DisableLCLGIF}  FPReadGif,  {$ENDIF}... ---------------------------------------------------------------------- KodeZwerg · 2024-04-03 16:25 It strong depend about what formats you want to support, some generic formats are supported by LCL for loading or displaying but that does not mean you can change all context however you liking it. Some more formats you can get by using the FreePascal sources. Best practice is to convert internal to bitmap format, modify it and convert back to source format. FPC/LCL also offers a special kind of internal storage format for images where I am not that skilled yet. A third party like BGRABitmap or the new PV_Bitmap may assist to support some more formats where the LCL has no support yet and both can manipulate the image on the fly. From my point of view, most formats having a fixed binary signature, pre-check a file for possible signatures and load/decode to bitmap, draw or modify pixels, offer via savedialog the destination format conversion. ---------------------------------------------------------------------- Tomi · 2024-04-05 13:16 Thank you for your answers, experts! Then, will TPicture be the solution? I will tried it, but I got an error message at CopyRect command, because it seems, TPicture has no canvas property. Can I use CopyRect command with TPicture? I would like perform copy action in my program with a piece of image. ---------------------------------------------------------------------- wp · 2024-04-05 13:45 TPicture has no canvas, yes. But it can provide access to the loaded images as TGraphic, TBitmap, TPortableNetworkGraphic, or as TJpegImage: Picture.Graphic, Picture.Bitmap, Picture.PNG, Picture.JPEG Untested (cuts the inner quarter of an image and saves that to a bitmap file): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ButtonClick(Sender: TObject);var  pic: TPicture;  bmp: TBitmap;  Rsrc, Rdest: TRect;begin  pic := TPicture.Create;  try    pic.LoadfromFile(NAME_OF_THE_PICTURE_FILE);    bmp := TBitmap.Create;    try      bmp.SetSize(pic.Width div 2, pic.Height div 2);      Rdest := Rect(0, 0, bmp.Width, bmp.Height);      Rsrc := Rect(0, 0, bmp.Width, bmp.Height);      OffsetRect(Rsrc, (pic.Width - bmp.Width) div 2, (pic.Height - bmp.Height) div 2);      bmp.Canvas.CopyRect(Rdest, pic.Graphic, Rsrc);      bmp.SaveToFile(NAME_OF_THE_OUTPUT_FILE);    finally      bmp.Free;    end;  finally    pic.Free;  end;end; ---------------------------------------------------------------------- Tomi · 2024-04-05 16:42 Something is wrong with the second parameter of this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bmp.Canvas.CopyRect(Rdest, pic.Graphic, Rsrc);"Incompatible type for arg no. 2: Got "TGraphic", expected "TCanvas"" Therefore I tried using TImage instead of TPicture, but in this case my program can use only BMPs... ---------------------------------------------------------------------- wp · 2024-04-05 17:30 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bmp.Canvas.CopyRect(Rdest, pic.Bitmap.Canvas, Rsrc); ---------------------------------------------------------------------- Tomi · 2024-04-06 08:39 Quote from: wp on April 05, 2024, 07:30:52 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bmp.Canvas.CopyRect(Rdest, pic.Bitmap.Canvas, Rsrc);  Thank you, wp; it works! Now the program can use not only BMPs, but e.g. JPGs too, exactly as I want.