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:
| function Thumbnail(InStream: TStream;Width, Height: Integer; FillColor: TColor=clWhite):TBitmap; var JpegImage: TJpegImage; Bitmap: TBitmap; Ratio: Double; ARect: TRect; AHeight, AHeightOffset: Integer; AWidth, AWidthOffset: Integer; begin if Width<1 then raise Exception.Create('Invalid Width'); if Height<1 then raise Exception.Create('Invalid Height'); JpegImage:=TJpegImage.Create; try JpegImage.LoadFromStream(InStream); Bitmap:=TBitmap.Create; try Ratio:=JpegImage.Width/JpegImage.Height; if Ratio>1 then begin AHeight:=Round(Width/Ratio); AHeightOffset:=(Height-AHeight) div 2; AWidth:=Width; AWidthOffset:=0; end else begin AWidth:=Round(Height*Ratio); AWidthOffset:=(Width-AWidth) div 2; AHeight:=Height; AHeightOffset:=0; end; Bitmap.Width:=Width; Bitmap.Height:=Height; Bitmap.Canvas.Brush.Color:=FillColor; Bitmap.Canvas.FillRect(Rect(0,0,Width,Height)); ARect:=Rect(AWidthOffset,AHeightOffset,AWidth+AWidthOffset,AHeight+AHeightOffset); Bitmap.Canvas.StretchDraw(ARect,JpegImage); Result := Bitmap; finally end; finally end;
end; |