Printing bitmaps in scale to both printer and the preview screen

Category

Code Based - General

Question

Printing bitmaps in scale to both printer and the preview screen

Solution

Following is a procedure that will print the bitmap in scale given a desired width OR a desired height. If one is given, the other should be zero, in order for the bitmap to be correctly scaled.

procedure PrintBitmapScaled(BaseReport: TBaseReport;
                              X1: double;
                              Y1: double;
                              DesiredWidth: double;
                              DesiredHeight: double;
                              Bitmap: TBitmap);

  var
    Calc: double;

  begin { PrintBitmapScaled }
    With BaseReport do begin
      If DesiredWidth <= 0.0 then begin
        Calc := DesiredHeight * (Bitmap.Width / XDPI) / (Bitmap.Height / YDPI);
        PrintBitmapRect(X1,Y1,X1 + Calc,Y1 + DesiredHeight,Bitmap);
      end else if DesiredHeight <= 0.0 then begin
        Calc := DesiredWidth * (Bitmap.Height / YDPI) / (Bitmap.Width / XDPI);
        PrintBitmapRect(X1,Y1,X1 + DesiredWidth,Y1 + Calc,Bitmap);
      end else begin
        PrintBitmapRect(X1,Y1,X1 + DesiredWidth,Y1 + DesiredHeight,Bitmap);
      end; { else }
    end; { with }
  end;  { PrintBitmapScaled }


An example call to the procedure would then be something like:

PrintBitmapScaled(Sender as TBaseReport,1.0,1.0,4.0,0.0,Bitmap);