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
| {$DEFINE RPICASSERT}
const
RPIC_SUCCESS = 0;
RPIC_NODIMENSION = 1;
RPIC_SRCNOTASSIGNED = 2;
RPIC_DSTNOTASSIGNED = 3;
RPIC_SRCEMPTY = 4;
RPIC_BUFFERERROR = 5;
RPIC_STRETCHERROR = 6;
RPIC_DESTOUTERROR = 7;
function RedimPic(Src, Dest: TGraphic; const NewWidth, NewHeight: LongInt): LongInt;
var NW, NH : LongInt;
R : Single;
BufferDest, BufferSrc : TBitmap;
begin
result := RPIC_NODIMENSION;
{$IFDEF RPICASSERT}
assert((NewWidth > 0) or (NewHeight > 0), 'Vous n''avez pas precisé de nouvelle dimensions pour l''image !');
{$ELSE}
if (NewWidth <= 0) and (NewHeight <= 0) then
exit;
{$ENDIF}
result := RPIC_SRCNOTASSIGNED;
{$IFDEF RPICASSERT}
assert(assigned(Src), 'Image source non assignée !');
{$ELSE}
if not assigned(Src) then
exit;
{$ENDIF}
result := RPIC_DSTNOTASSIGNED;
{$IFDEF RPICASSERT}
assert(assigned(Dest), 'Image de destination non assignée !');
{$ELSE}
if not assigned(Dest) then
exit;
{$ENDIF}
result := RPIC_SRCEMPTY;
{$IFDEF RPICASSERT}
assert(not Src.Empty, 'Image source est vide !');
{$ELSE}
if Src.Empty then
exit;
{$ENDIF}
if (NewWidth > 0) and (NewHeight > 0) then
begin
NW := NewWidth;
NH := NewHeight;
end
else
if NewWidth > 0 then
begin
R := 1 - NewWidth / Src.Width;
NW := trunc(Src.Width * R);
NH := trunc(Src.Height * R);
end
else
if NewHeight > 0 then
begin
R := 1 - NewHeight / Src.Height;
NW := trunc(Src.Width * R);
NH := trunc(Src.Height * R);
end;
result := RPIC_BUFFERERROR;
BufferDest := TBitmap.Create;
BufferSrc := TBitmap.Create;
try
BufferSrc.Width := Src.Width;
BufferSrc.Height := Src.Height;
BufferSrc.Canvas.Draw(0, 0, Src);
BufferDest.Width := NW;
BufferDest.Height := NH;
result := RPIC_STRETCHERROR;
SetStretchBltMode(BufferDest.Canvas.Handle, HALFTONE);
StretchBlt(BufferDest.Canvas.Handle, 0, 0, NW, NH,
BufferSrc.Canvas.Handle, 0, 0, BufferSrc.Width, BufferSrc.Height,
SRCCOPY);
result := RPIC_DESTOUTERROR;
Dest.Assign(BufferDest);
finally
result := RPIC_BUFFERERROR;
BufferSrc.Free;
BufferDest.Free;
end;
result := RPIC_SUCCESS;
end; |
Partager