# Algo needed for 'Fit to page'

• 10-02-2002
Unregged
Algo needed for 'Fit to page'
Hello :)

Hi, sorry for just dropping in... Does anyone know how to calculate a 'Fit To Page' thing when printing.

I don't need no source. Just the basic equation would be great.

TIA,
Jim
• 10-02-2002
sean345
Take a look at Petzold's Book Programming Windows. It has several ways of printing using Windows API.

- Sean
• 10-02-2002
Unregged
Sean,

I know how to print.. and I read Petzold a long time ago. My problem is mathematics ;)

I do have the the widths and heights (let's call them:)

bmp.x *** width of the bitmap
bmp.y *** height of the bitmap
pd.x *** width of the printer DC
pd.y *** height of the printer DC

I just need to figure out that if I take the longest side of the bitmap (ex: width) and set this side the same value as the printers width. how to calculate the propper porpotion of it's lenght for the printer. Considering that sometimes the bitmap needs to be shrinked and sometimes enlarged.

Like saying the width of the bitmap (20000 pixels) == width of the printer (7000 pixels) So shrink it by 13000 pixels) Which is ??% of it. Considering this I need to shrink the height by the same amount of % to keep the propper porpotions.

How do I do % calculations like this manually ? Using Divide, Substraction and Multiplication.

Wish I had paid more attention in school that day :)
• 10-02-2002
Unregged
Forget it ;)

I just posted my answer myself... ( LOL )

IF bmp.x > pd.x
percentage.to.shrink = (bmp.x/100)*(bmp.x-pd.x)

ELSE
percentage.to.enlarge = (bmp.x/100)*(pd.x-bmp.x)

ENDIF
• 10-02-2002
Unregged
Here I post my 'pseudo code' in case someone ever needs 'Fit-To-Page' calculations... Of course this is mostly assumptions, but it looks pretty correct atm.

Code:

``` /// bmp.x == width of bitmap bmp.y == height of bitmap pd.x == pd.original.width == printerDC.width pd.y == pd.original.height == printerDC.height /// .IF bmp.x > bmp.y   .IF bmp.x >= pd.x             percentage.to.enlarge == (bmp.x/100) * (bmp.x - pd.x)       pixels == (bmp.y/100)*percentage.to.enlarge)       pd.y == bmp.y + pixels   .ELSE       percentage.to.shrink == (bmp.x/100) * (pd.x - bmp.x)       pixels == (bmp.y/100)*percentage.to.shrink)       pd.y == bmp.y - pixels   .ENDIF ////at this point we can be sure that the width is fitting !   .IF pd.y > pd.original.height       percentage.to.shrink == (pd.original.height/100)*(pd.y-pd.original.height)       pixels == (pd.y/100)*percentage.to.shrink       pd.y == pd.y - pixels       pixels == (pd.x/100)*percentage.to.shrink       pd.x == pd.x - pixels   .ENDIF .ELSE   .IF bmp.y >= pd.y       percentage.to.enlarge == (bmp.y/100) * (bmp.y - pd.y)       pixels == (bmp.x/100)*percentage.to.enlarge)       pd.x == bmp.x + pixels   .ELSE       percentage.to.shrink == (bmp.y/100) * (pd.y - bmp.y)       pixels == (bmp.x/100)*percentage.to.shrink)       pd.x == bmp.x - pixels   .ENDIF ////at this point we can be sure that the height is fitting !   .IF pd.x > pd.original.width       percentage.to.shrink == (pd.original.width/100)*(pd.x - pd.originalwidth)       pixels == (pd.x/100)*percentage.to.shrink       pd.x == pd.x - pixels       pixels == (pd.y/100)*percentage.to.shrink       pd.y == pd.y - pixels   .ENDIF .ENDIF```
If someone sees an error feel free to correct :-P
• 10-02-2002
novacain
Using StretchBlt() is slow and unlikely to give required results unless an integer multiplier is used.

A better solution is to use Coordinate Space Transformations.

Experiment with this code (will fail if iImageWidth or iImageHeight is greater than the client area)

Code:

```SetGraphicsMode(hdc, GM_ADVANCED);//only works with WIN2000 (not for WIN98) SetMapMode(hdc,MM_ISOTROPIC);//preserve image ratio SetWindowExtEx  (hdc, iWidth, iHeight, NULL) ;//the arbitrary scale to use SetViewportExtEx (hdc, iActualWidth, iActualHeight, NULL) ;//the actual client area xForm.eM11 = (FLOAT) iRatio; //set the ratio to change image size xForm.eM12 = (FLOAT) 0.0; xForm.eM21 = (FLOAT) 0.0; xForm.eM22 = (FLOAT) iRatio; xForm.eDx  = (FLOAT) 0.0; xForm.eDy  = (FLOAT) 0.0; SetWorldTransform(hdc, &xForm); //calc the margins (could fail here) Rect.left=((ClientRect.right-ClientRect.left)- iImageWidth)/2; Rect.top=((ClientRect.bottom-ClientRect.top)- iImageHeight)/2; Rect.right= Rect.left +iImageWidth; Rect.bottom= Rect.top+iImageHeight;         //convert to DC scale DPtoLP (hdc, &Rect, 2) ; //draw the rect to show image size hBrush=CreateSolidBrush(RGB(0,0,0));//black brush hSysBrush=SelectObject(hdc,hBrush); FrameRect(hdc,&Rect,hBrush); SelectObject(hdc,hSysBrush); DeleteObject(hBrush);```
• 10-03-2002
Unregged
Novacain,

Thanks for the reply. But even tho that I developp this app for XP I try to keep it downwards compatible. (Also because I refuse to put anything else then 98SE on my developpment box :grin: )

Actually I don't use StretchBlt but iPicture::Render for the job as I blend a JPG onto the PrintersDC :) but it's basically the same as StretchBlt.

Anyway, I play around with your code for awhile and see how far I get :)

Regards,
Jim