TOpenPictureDialog OS Depending ? lazarus / 31-LCL ---------------------------------------------------------------------- BeanzMaster · 2020-07-22 15:05 Hi to all, i have a little question about TOpenPictureDialog. I would like to improve the preview with my owns methods. (for previewing images like pcx, tga, webp....) I just simpy herited my own dialog from TOpenPictureDialog like 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 type  TBZOpenPictureDialog = Class(TOpenPictureDialog)  private    FPreviewFilename, FSaveTitle : string;  protected    procedure UpdatePreview; override;  public    function Execute: boolean; override;  end; implementation procedure TBZOpenPictureDialog.UpdatePreview;var  CurFilename: string;  FileIsValid: boolean;  Bmp : TBZBitmap;  NativeBmp : Graphics.TBitmap; begin  CurFilename := FileName;   if CurFilename = FPreviewFilename then exit;  FPreviewFilename := CurFilename;  FileIsValid := FileExists(FPreviewFilename); //FileIsReadable(FPreviewFilename);  if FileIsValid then  try    Bmp := TBZBitmap.create;    try      Bmp.LoadFromFile(FPreviewFilename);    PictureGroupBox.Caption := Format('(%s - %dx%d BPP:%d)',                               [Bmp.DataFormatDesc.Desc, Bmp.Width, Bmp.Height, Bmp.ImageDescription.BitCount]);      Bmp.Transformation.Stretch(ImageCtrl.ClientWidth, ImageCtrl.ClientHeight, True);       NativeBmp := Bmp.ExportToBitmap;      ImageCtrl.Transparent := False;      ImageCtrl.Picture.Bitmap.Assign(NativeBmp);      ImageCtrl.Repaint;     FreeAndNil(NativeBmp)     finally      FreeAndNil(Bmp)    end;  except    FileIsValid := False;  end;  if not FileIsValid then  begin    ShowMessage('Not Valid');    ClearPreview;   end;end; function TBZOpenPictureDialog.Execute: boolean;begin  Filter := GetBZImageFileFormats.BuildDialogFilters;  FSaveTitle :=  Self.Title;  result := inherited Execute;end;           But it's not working, I'm on Windows. So i suppose TOpenPictureDialog depend on the OS API So, in this case,  my question What is the code for UpdatePreview ? because the TGroupbox and Timage, is never show and didn't took in account. It is possible, without recreate the wheel ? Thanks in advance Cheers ---------------------------------------------------------------------- jamie · 2020-07-22 22:49 My docs say it's "PreviewFileControl" in the dialog is what you need to paint on... you may need to create a canvas for that..  ACanvas.Handle := GetDC(PreviewFileCOntrol.Handle);    now do your code.. Make sure you release the DC handle afterwards. ---------------------------------------------------------------------- BeanzMaster · 2020-07-23 07:36 Quote from: jamie on July 23, 2020, 12:49:24 amMy docs say it's "PreviewFileControl" in the dialog is what you need to paint on... you may need to create a canvas for that..  ACanvas.Handle := GetDC(PreviewFileCOntrol.Handle);    now do your code.. Make sure you release the DC handle afterwards. Hi Jamie, thanks but i'm not understanding how do this, and why pass through Handle. Because TOpenPictureDialog is declared  as this : The GroupBox and TImage are created and we can get access directly, normally.  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 Type TOpenPictureDialog = class(TPreviewFileDialog)  private    FDefaultFilter: string;    FImageCtrl: TImage;    FPictureGroupBox: TGroupBox;    FPreviewFilename: string;  protected    class procedure WSRegisterClass; override;    function  IsFilterStored: Boolean; virtual;    property ImageCtrl: TImage read FImageCtrl;    property PictureGroupBox: TGroupBox read FPictureGroupBox;    procedure InitPreviewControl; override;    procedure ClearPreview; virtual;    procedure UpdatePreview; virtual;  public    constructor Create(TheOwner: TComponent); override;    procedure DoClose; override;    procedure DoSelectionChange; override;    procedure DoShow; override;    function GetFilterExt: String;    property DefaultFilter: string read FDefaultFilter;  published    property Filter stored IsFilterStored;  end;                implementation constructor TOpenPictureDialog.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);  FDefaultFilter := GraphicFilter(TGraphic)+'|'+                       Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,'']);  Filter:=FDefaultFilter;   FPictureGroupBox:=TGroupBox.Create(Self);  with FPictureGroupBox do begin    Name:='FPictureGroupBox';    Align:=alClient;  end;   FImageCtrl:=TImage.Create(Self);  with FImageCtrl do begin    Name:='FImageCtrl';    Parent:=FPictureGroupBox;    Align:=alClient;    Center:=true;    Proportional:=true;  end;end; procedure TOpenPictureDialog.ClearPreview;begin  FPictureGroupBox.Caption:='None';  FImageCtrl.Picture:=nil;end; procedure TOpenPictureDialog.UpdatePreview;var  CurFilename: String;  FileIsValid: boolean;begin  CurFilename := FileName;  if CurFilename = FPreviewFilename then exit;   FPreviewFilename := CurFilename;  FileIsValid := FileExistsUTF8(FPreviewFilename)                 and (not DirPathExists(FPreviewFilename))                 and FileIsReadable(FPreviewFilename);  if FileIsValid then    try      FImageCtrl.Picture.LoadFromFile(FPreviewFilename);      FPictureGroupBox.Caption := Format('(%dx%d)',        [FImageCtrl.Picture.Width, FImageCtrl.Picture.Height]);    except    FileIsValid := False;    end;  if not FileIsValid then    ClearPreview;end; So, i don't understand why my code doesn't work. And Why TOpenPictureDialog 's code is like this and how it work ???? I need to declare/attach my own dialog somewhere or with a method that i don't know, to the widgetset ??? Thanks Cheers ---------------------------------------------------------------------- BeanzMaster · 2020-07-23 08:13 I have made little test, just by adding a form for previewing picture 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 typeunit BZPictureDialogs; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Graphics, GraphType, InterfaceBase, LCLType,  IntfGraphics, FPimage, LCLIntf, LazFileUtils,  Forms, ExtDlgs , StdCtrls, ExtCtrls, Dialogs,   BZGraphic, BZBitmap, BZBitmapIO;      { TBZOpenPictureDialog }  TBZOpenPictureDialog = Class(TOpenPictureDialog)  private    FPreviewFilename, FSaveTitle : string;    FPreviewForm : TForm;  protected    procedure UpdatePreview; override;  public    constructor Create(TheOwner: TComponent); override;    procedure DoClose; override;    function Execute: boolean; override;  end; implementation  procedure TBZOpenPictureDialog.UpdatePreview;var  CurFilename: string;  FileIsValid: boolean;  Bmp : TBZBitmap;  NativeBmp : Graphics.TBitmap; begin   CurFilename := FileName;   if CurFilename = FPreviewFilename then exit;  FPreviewFilename := CurFilename;  FileIsValid := FileExists(FPreviewFilename); //FileIsReadable(FPreviewFilename);  if FileIsValid then  try    Bmp := TBZBitmap.create;    try      Bmp.LoadFromFile(FPreviewFilename);    PictureGroupBox.Caption := Format('(%s - %dx%d BPP:%d)',                               [Bmp.DataFormatDesc.Desc, Bmp.Width, Bmp.Height, Bmp.ImageDescription.BitCount]);      Bmp.Transformation.Stretch(ImageCtrl.ClientWidth, ImageCtrl.ClientHeight, True);       NativeBmp := Bmp.ExportToBitmap;      ImageCtrl.Transparent := False;      ImageCtrl.Picture.Bitmap.Assign(NativeBmp);      ImageCtrl.Repaint;     FPreviewForm.Caption := FSaveTitle + ' - ' + ExtractFileName(FPreviewFilename) + ' : ' + PictureGroupBox.Caption;    FPreviewForm.Show;      bmp.DrawToCanvas(FPreviewForm.Canvas, FPreviewForm.ClientRect);       FreeAndNil(NativeBmp)     finally    FreeAndNil(Bmp)    end;  except    FileIsValid := False;  end;  if not FileIsValid then  begin    ShowMessage('Not Valid');    FPreviewForm.Close;    ClearPreview;   end;end; function TBZOpenPictureDialog.Execute: boolean;begin  Filter := GetBZImageFileFormats.BuildDialogFilters;  FSaveTitle :=  Self.Title;  result := inherited Execute;end; constructor TBZOpenPictureDialog.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);  FPreviewForm := TForm.Create(nil);  FPreviewForm.Width := 320;  FPreviewForm.Height := 240;  FPreviewForm.Position :=  poScreenCenter; end; procedure TBZOpenPictureDialog.DoClose;begin  FPreviewForm.Close;  FreeAndNil(FPreviewForm);  inherited DoClose;end;         It work, picture are showing Like we see on the attached GIF. It seems  "ImageCtrl" is not used because it have the default dimensions 60x60 and the preview in dialog itself is not shown. Thanks in advance for your help Cheers Jérôme