Wo liegt der Denkfehler?
Ich will mein Bitmap skalieren, falls es größer ist als der Druckbereich. Dafür rechne ich mir die Seitengröße und den nicht bedruckbaren Bereich aus und erhalte dann PrintableAreaX bzw -Y. Falls diese Zahl also größer ist als die Bmp.Width bzw Bmp.Height kann ganz normal gezeichnet werden, ansonsten soll er aber skalieren.
Bei 300 dpi ist noch alles in Ordnung und ich dachte schon alles würde wunderbar funktionieren, jetzt ist mir 600 dpi allerdings mal aufgefallen, dass er garnich in den Else-Teil reingeht, weil PrintableAreaX bzw. -Y immer größer ist als die Bitmaphöhe oder -breite. Das kann teilweise aber dann garnich stimmen, weil er ja im Ausdruck mir die Hälfte des Bildes abschneidet.
Hab ich irgendwo was vergessen? Wäre toll, wenn mir da einer helfen könnte!!!
Delphi-Quellcode:
class function MillimeterPerPixel(MM: Integer): Integer;
var
PrinterDPI: Integer;
begin
PrinterDPI := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
Result := Round((PrinterDPI / 25.4) * MM);
end;
procedure Print;
var
Bmp: TBitmap;
X: Integer;
Y: Integer;
Rect: TRect;
BmpWidth: Integer;
BmpHeight: Integer;
PageWidth: Integer;
PageHeight: Integer;
PrintableAreaX: Integer;
PrintableAreaY: Integer;
Scale: Double;
DiffHeight: Integer;
DiffWidth: Integer;
begin
if (PrintDialog.Execute) then
begin
try
Printer.BeginDoc;
Printer.Title := 'Treemap';
Bmp := TBitmap.Create;
Bmp.LoadFromFile('Bitmap.bmp');
// 1 pixel = 0,1 mm
SetMapMode(Printer.Canvas.Handle, MM_LOMETRIC);
// get bmp measurements
BmpWidth := Round(MillimeterPerPixel(Bmp.Width) / 10);
BmpHeight := Round(MillimeterPerPixel(Bmp.Height) / 10);
// get page margin
X := Round((GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX) * MillimeterPerPixel(1)) / 10);
Y := Round((GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY) * MillimeterPerPixel(1)) / 10);
// page size
PageWidth := Round((GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) * MillimeterPerPixel(1))/10);
PageHeight := Round((GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT) * MillimeterperPixel(1))/10);
// get printable area
PrintableAreaX := PageWidth - (2*X);
PrintableAreaY := PageHeight - (2*Y);
// new starting point
SetWindowOrgEx(Printer.Handle, X, -Y, nil);
// define bounding rectangle
Rect.TopLeft := Point(X, -Y);
// if bmp height or width is greater than the printable area, it has to be scaled into page size
if ((PrintableAreaX >= BmpWidth + X) and (PrintableAreaY >= BmpHeight + Y)) then
Rect.BottomRight := Point(BmpWidth+X, -(BmpHeight+Y))
else if ((PrintableAreaX >= BmpWidth + X) and (PrintableAreaY < BmpHeight + Y)) then
begin
Rect.BottomRight.Y := PrintableAreaY;
Scale := BmpHeight / PrintableAreaY;
Rect.BottomRight.X := Round(BmpWidth / Scale);
end
else if ((PrintableAreaX < BmpWidth + X) and (PrintableAreaY >= BmpHeight + Y)) then
begin
Rect.BottomRight.X := PrintableAreaX;
Scale := BmpWidth / PrintableAreaX;
Rect.BottomRight.Y := Round(BmpHeight / Scale);
end
else if ((PrintableAreaX < BmpWidth + X) and (PrintableAreaY < BmpHeight + Y)) then
begin
DiffWidth := BmpWidth - PrintableAreaX;
DiffHeight := BmpHeight - PrintableAreaY;
if(DiffHeight > DiffWidth) then
begin
Rect.BottomRight.Y := PrintableAreaY;
Scale := BmpHeight / PrintableAreaY;
Rect.BottomRight.X := Round(BmpWidth / Scale);
end
else
begin
Rect.BottomRight.X := PrintableAreaX;
Scale := BmpWidth / PrintableAreaX;
Rect.BottomRight.Y := Round(BmpHeight / Scale);
end;
end;
// print
Printer.Canvas.StretchDraw(Rect, Bmp);
finally
Printer.EndDoc;
Bmp.Free;
end;//try..finally
end//if(PrintDialog...)
else
exit;
end;
Alles Liebe,
daschaos