From: "Chris Rorden" To: Subject: computer lab note Date: Monday, August 14, 2000 12:51 PM Hi- I wanted to find out how to print JPEGs, and your site was (as usual) the first place I looked. Thanks for an excellent archive of useful routines. I did find one minor problem with your routine ButtonPrintClick, which is at: http://www.efg2.com/Lab/Library/Delphi/Printing/PrintJPEGTImage.txt The procedure scales the image so that the image width takes up 76% of the paper width, and uses a landscape orientation. This procedure can fail if either of these common occurences are in place: 1.) the image height is much taller than the image width 2.) the paper is rectangular, not square For example, with standard paper (European A4 paper is slightly less square than US 8.5x11), printing a square image can have the top and bottom of the image trimmed. Below I am sending you a variation of the code which fixes this. It adds two bits of code: 1.) it checks whether the IMAGE is taller or wider, and selects landscape or portrait to suit. 2.) it then checks the IMAGE aspect ratio versus the PRINTER aspect ratio, to see whether height or width is the constraint on scaling. -chris p.s. I am not trying to complain here - it is great you have posted such a great set of tools. I also realize that it is important to distribute minimalistic code to teach concepts. It is possible that my code would confuse more people than it would help. procedure TForm1.ButtonPrintClick(Sender: TObject); VAR Bitmap : TBitmap; liHt,liWid,iFromLeftMargin,iPrintedImageWidth,jDelta,jFromTopOfPage,jPrintedImageHeight: INTEGER; begin liHt := Image1.Picture.Height; liWid := Image1.Picture.Width; if (liHt = 0) or (liWid = 0) then exit; Screen.Cursor := crHourGlass; TRY if liHt > liWid then Printer.Orientation := poPortrait //image is taller than wide else Printer.Orientation := poLandscape; //image is wider than tall Printer.BeginDoc; Printer.Canvas.Font.Height := // fixed 28 Aug 2000 MulDiv(GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY), 8, 72); Printer.Canvas.Font.Name := 'Arial'; jDelta := Printer.Canvas.TextHeight('X'); jFromTopOfPage := 3*jDelta; s := 'Image Title'; Printer.Canvas.TextOut(CenterText(s), jFromTopOfPage, s); // 5th line from top jFromTopOfPage := 5*jDelta; // Image position and size // 12% left and right margin iFromLeftMargin := MulDiv(Printer.PageWidth,12,100); // 12% if (liHt/liWid) > (Printer.PageHeight/Printer.PageWidth) then begin //Paper HEIGHT is constrained aspect ration // Set printed bitmap with to be 76% of paper HEIGHT jPrintedImageHeight := MulDiv(Printer.PageHeight,76,100); // 76% // Set printed bitmap WIDTH to maintain aspect ratio iPrintedImageWidth:= liWid*jPrintedImageHeight DIV liHt; // maintain aspect ratio of bitmap end else begin //Paper WIDTH is constrained aspect ration // Set printed bitmap with to be 76% of paper WIDTH iPrintedImageWidth := MulDiv(Printer.PageWidth,76,100); // 76% // Set printed bitmap height to maintain aspect ratio jPrintedImageHeight := liHt*iPrintedImageWidth DIV liWid; end; Bitmap := TBitmap.Create; TRY Bitmap.Width := liWid; Bitmap.Height := liHt; Bitmap.PixelFormat := pf24bit; // Convert JPEG to BMP Bitmap.Canvas.Draw(0,0,Image1.Picture.Graphic); PrintBitmap (Printer.Canvas, Rect(iFromLeftMargin, jFromTopOfPage, iFromLeftMargin + iPrintedImageWidth, jFromTopOfPage + jPrintedImageHeight), Bitmap) FINALLY Bitmap.Free END; Printer.EndDoc; FINALLY Screen.Cursor := crDefault END; end; __________________________________________ Dr. Chris Rorden Department of Psychology University of Nottingham Nottingham NG7 2RD, UK Tel: +44 [0]115-951-5294 Fax: +44 [0]115-951-5324 http://www.psychology.nottingham.ac.uk/staff/cr1/