1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| uses
FPReadJPEG, // pour AfficheJpeg
LCLtype, IntfGraphics;
const
IMGMARGINS = 10;
var
ImgBigHeight,ImgBigWidth: integer;
FileWidth, FileHeight: integer;
procedure TfrmBigDisplay.SetupWindow(img: string); // img : nom du fichier jpg avec chemin
function AfficheJPEG: Boolean;
var
InStream: TFileStream;
function DisplayJpeg(const InFileName: TFileStream; Width, Height: Integer): Boolean;
var
JpegImage: TJpegImage;
Bitmap: TBitmap;
AHeight,AWidth : Integer;
begin
Result := False;
JpegImage := TJpegImage.Create;
JpegImage.PixelFormat := pf24Bit;
//JpegImage.Performance:= jpBestQuality; // /!\
JpegImage.Performance:= jpBestSpeed; // /!\
try
// Load the image
JpegImage.LoadFromStream(InFileName);
JpegImage.GetSize(FileWidth, FileHeight);
// Create bitmap, and calculate parameters
Bitmap := TBitmap.Create;
try
SetupImgForDisplay;
imgBigDisplay.Picture.Assign(JpegImage);
Result := True;
finally
Bitmap.Free;
JpegImage.Free;
end;
except
on E: Exception do
MessageDlg('Erreur','Erreur: '+E.Message,mtError,[mbOk],0);
end;
end;
begin
InStream := TFileStream.Create(img, fmOpenRead);
Result := DisplayJpeg(InStream, ImgBigWidth, ImgBigHeight);
InStream.Free;
end;
begin
imgBigDisplay.Picture.Bitmap := nil; // effacement avant nouveau traitement
AfficheJPEG;
end;
procedure TfrmBigDisplay.SetupImgForDisplay; // appelé par FormResize
var
Ratio1, Ratio2: Double;
ImagePos: TPoint;
begin
Ratio1 := FileWidth/FileHeight;
Ratio2 := ImgBigWidth/ImgBigHeight;
if ((FileWidth > imgBigWidth) or (FileHeight > imgBigHeight)) then
begin
if Ratio1 > Ratio2 then
begin
imgBigDisplay.Width := imgBigWidth;
imgBigDisplay.Height:= round(imgBigDisplay.Width / Ratio1);
end
else
begin
imgBigDisplay.Height:= imgBigHeight;
imgBigDisplay.Width := round(imgBigDisplay.Height * Ratio1);
end;
end
else // petites images
begin
imgBigDisplay.Width := FileWidth;
imgBigDisplay.Height := FileHeight;
end;
// Centrer image dans container
ImagePos := Point( (pnlSupportImg.Width - imgBigDisplay.Width ) div 2,
(pnlSupportImg.Height - imgBigDisplay.Height) div 2 );
// test si la position est négative (si image > à container)
if ImagePos.X < 0 then ImagePos.X := 0;
if ImagePos.Y < 0 then ImagePos.Y := 0;
imgBigDisplay.Left:= ImagePos.X;
imgBigDisplay.Top := ImagePos.Y;
end;
procedure TfrmBigDisplay.FormResize(Sender: TObject);
begin
SetupContainer;
if imgBigDisplay.Picture.Bitmap <> nil then SetupImgForDisplay;
end;
procedure TfrmBigDisplay.SetupContainer; // appelé par FormCreate et FormResize
begin
// taille du compo container :
imgBigWidth := pnlSupportImg.Width -IMGMARGINS;
imgBigHeight:= pnlSupportImg.Height-IMGMARGINS;
end; |
Partager