Impressao - milimetro para pixels |
Top Previous Next |
|
// Imprimir com precisão milimétrica // O objeto Canvas que está na classe Printer é uma ferramenta que ajuda muito // a imprimir qualquer tipo de dados, sejam eles texto ou gráficos. // O problema é que a largura e a altura são determinadas em pixels, e esses // valores variam de acordo com a resolução da impressora. // Para converter de milímetros para pixels, use as funções abaixo, sendo que // MMtoPixelX é para a resolução horizontal e MMtoPixelY é para a resolução // vertical (porque na impressora é possível uma resolução como 1440x720 dpi - // 1440 dpi para a horizontal e 720 dpi para a vertical, por exemplo):
function MMtoPixelX(const MM: Integer): Longint; var mmPointX : Double; PageSize, OffSetUL: TPoint; begin mmPointX := Printer.PageWidth / GetDeviceCaps(Printer.Handle, HORZSIZE); Escape(Printer.Handle,GETPRINTINGOFFSET,0,nil,@OffSetUL); Escape(Printer.Handle,GETPHYSPAGESIZE,0,nil,@PageSize); if MM > 0 then Result := Round((MM * mmPointX) - OffSetUL.X) else Result := Round(MM * mmPointX); end;
function MMtoPixelY(const MM: Integer): Longint; var mmPointY : Double; PageSize, OffSetUL: TPoint; begin mmPointY := Printer.PageHeight / GetDeviceCaps(Printer.Handle,VERTSIZE); Escape(Printer.Handle,GETPRINTINGOFFSET,0,nil,@OffSetUL); Escape(Printer.Handle,GETPHYSPAGESIZE,0,nil,@PageSize); if MM > 0 then Result := Round((MM * mmPointY) - OffSetUL.Y) else Result := Round(MM * mmPointY); end;
// -------------- Este eu peguei na net function PixelsToCm(Pixels, PixelsPerInch: Integer): Double; begin { Não me lembro bem mas acho que uma polegada tem 2.54 cm... se for diferente, mude } Result := (Pixels * 2.54) / PixelsPerInch; end;
Para pegar o número de pixels por polegada da tela, use:
var DC: HDC; PixelsPerInchH, PixelsPerInchV: Integer;
CmH, CmV: Double; begin DC := GetDC(0); try PixelsPerInchH := GetDeviceCaps(DC, LOGPIXELSX); PixelsPerInchV := GetDeviceCaps(DC, LOGPIXELSY); finally ReleaseDC(0, DC); end; { Então, para saber quantos pixels tem uma imagem use algo assim: } CmH := PixelsToCm(Bmp.Width, PixelsPerInchH); CmV := PixelsToCm(Bmp.Width, PixelsPerInchV); end;
Para pegar o número de pixels por polegada da impressora, use:
var PixelsPerInchH, PixelsPerInchV: Integer;
CmH, CmV: Double; begin PixelsPerInchH := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX); PixelsPerInchV := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY); end; |