Everything is good except sometimes depending on thumbnail width the thumbnails are clipped on the right edge of the page. The clipped images do not move to the next row until they are completely off the page right margin. See attached image of clipping.
I also modified ThumbnailDraw to optionally draw a thumbnail caption:
- Code: Select all
ThumbnailDraw ( BMP.Canvas, BMP.Canvas.ClipRect, fImageWidth, fImageHeight, fBorder, GraphicsArray, CaptionArray );
Here is the modified method. Do I have it correct?
- Code: Select all
function ThumbnailDraw ( Canvas: TCanvas;const Rect: TRect;
ThumbWidth, ThumbHeight, Margin: Integer;
Graphics: array of TGraphic;Caption: array of string ): Integer;
function ShrinkRect ( const R: TRect;G: TGraphic ): TRect;
var
iW, iH: Integer;
rW, rH: Integer;
begin
iW := G.Width;
iH := G.Height;
rW := R.Right - R.Left;
rH := R.Bottom - R.Top;
if ( iW > rW ) or ( iH > rH ) then // Shrink only
begin
if ( rW / iW ) < ( rH / iH ) then
begin
iH := MulDiv ( iH, rW, iW );
iW := rW;
end
else
begin
iW := MulDiv ( iW, rH, iH );
iH := rH;
end;
end;
SetRect ( Result, 0, 0, iW, iH );
OffsetRect ( Result, R.Left + ( rW - iW ) div 2, R.Top + ( rH - iH ) div 2 );
end;
var
ThumbRect: TRect;
ClipRgn: THandle;
I: Integer;
begin
Result := 0;
ClipRgn := CreateRectRgnIndirect ( Rect );
try
SelectClipRgn ( Canvas.Handle, ClipRgn );
finally
SelectClipRgn ( Canvas.Handle, 0 );
end;
SetRect ( ThumbRect, 0, 0, ThumbWidth, ThumbHeight );
OffsetRect ( ThumbRect, Rect.Left, Rect.Top );
for I := Low ( Graphics ) to High ( Graphics ) do
begin
Canvas.StretchDraw ( ShrinkRect ( ThumbRect, Graphics[I] ), Graphics[I] );
Canvas.Font.Name := 'Arial';
Canvas.Font.Size := 8;
Canvas.TextOut ( ThumbRect.Left + Canvas.Font.Size, ThumbRect.Bottom + 1, Caption[I] );
OffsetRect ( ThumbRect, ThumbWidth + Margin, 0 );
Inc ( Result );
if ThumbRect.Left >= Rect.Right then
begin
OffsetRect ( ThumbRect, Rect.Left - ThumbRect.Left, ThumbWidth + Margin );
if ThumbRect.Top > Rect.Bottom then Break;
end;
end;
SelectClipRgn ( Canvas.Handle, 0 );
end;
I am using ThumbnailDraw in a CreateContactSheet procedure:
- Code: Select all
procedure TForm1.CreateContactSheet;
var
BMP: TBitmap;
TempBMP: TBitmap;
I: integer;
GraphicsArray: array of TGraphic;
CaptionArray: array of string;
PageWidth: integer;
PageHeight: integer;
MarginLeft: integer;
MarginRight: integer;
MarginTop: integer;
MarginBottom: integer;
PageClientWidth: integer;
PageClientHeight: integer;
BMPWidth: integer;
BMPHeight: integer;
begin
BMP := TBitmap.Create;
try
PageWidth := PageSetupDialog1.PageWidth;
PageHeight := PageSetupDialog1.PageHeight;
MarginLeft := PageSetupDialog1.MarginLeft;
MarginRight := PageSetupDialog1.MarginRight;
MarginTop := PageSetupDialog1.MarginTop;
MarginBottom := PageSetupDialog1.MarginBottom;
PageClientWidth := PageWidth - ( MarginLeft + MarginRight );
PageClientHeight := PageHeight - ( MarginTop + MarginBottom );
BMPWidth := ConvertUnits ( PageClientWidth, Screen.PixelsPerInch, mmHiEnglish, mmPixel );
BMPHeight := ConvertUnits ( PageClientHeight, Screen.PixelsPerInch, mmHiEnglish, mmPixel );
BMP.Width := BMPWidth;
BMP.Height := BMPHeight;
BMP.PixelFormat := pf24bit;
TempBMP := TBitmap.Create;
SetLength ( GraphicsArray, ImageEnMView1.ImageCount );
SetLength ( CaptionArray, ImageEnMView1.ImageCount );
for i := 0 to ImageEnMView1.ImageCount - 1 do begin
TempBMP := ImageEnMView1.GetBitmap ( I );
GraphicsArray[I] := TempBMP;
CaptionArray[I] := ImageEnMView1.ImageTopText[I].Caption;
end;
fBorder := UpDown7.Position;
fImageWidth := UpDown1.Position;
fImageHeight := UpDown6.Position;
ThumbnailDraw ( BMP.Canvas, BMP.Canvas.ClipRect, fImageWidth, fImageHeight, fBorder, GraphicsArray, CaptionArray );
frmContactSheet.ImageEnView1.Blank;
frmContactSheet.ImageEnView1.IEBitmap.Assign ( BMP );
frmContactSheet.ImageEnView1.Update;
finally BMP.Free; end;
ImageEnMView1.ReleaseBitmap ( 0 );
end;
Questions:
1. Is the revised ThumbnailDraw function correct?
2. Can you fix the ThumbnailDraw method to prevent clipping?
3. Is there an easier way to set the bitmap width and height in the CreateContactSheet method?;
BMPWidth := ConvertUnits ( PageClientWidth, Screen.PixelsPerInch, mmHiEnglish, mmPixel );
BMPHeight := ConvertUnits ( PageClientHeight, Screen.PixelsPerInch, mmHiEnglish, mmPixel );
Thanks for the help.