Thread: Algo needed for 'Fit to page'

  1. #1

    Question Algo needed for 'Fit to page'


    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.


  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Take a look at Petzold's Book Programming Windows. It has several ways of printing using Windows API.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3



    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

  4. #4
    Forget it

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

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

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


  5. #5

    Red face

    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.

    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
 == (bmp.x/100) * (bmp.x - pd.x)
          pixels == (bmp.y/100)*
          pd.y == bmp.y + pixels
 == (bmp.x/100) * (pd.x - bmp.x)
          pixels == (bmp.y/100)*
          pd.y == bmp.y - pixels
    ////at this point we can be sure that the width is fitting !
       .IF pd.y > pd.original.height
 == (pd.original.height/100)*(pd.y-pd.original.height)
          pixels == (pd.y/100)*
          pd.y == pd.y - pixels
          pixels == (pd.x/100)*
          pd.x == pd.x - pixels
       .IF bmp.y >= pd.y
 == (bmp.y/100) * (bmp.y - pd.y)
          pixels == (bmp.x/100)*
          pd.x == bmp.x + pixels
 == (bmp.y/100) * (pd.y - bmp.y)
          pixels == (bmp.x/100)*
          pd.x == bmp.x - pixels
    ////at this point we can be sure that the height is fitting !
       .IF pd.x > pd.original.width
 == (pd.original.width/100)*(pd.x - pd.originalwidth)
          pixels == (pd.x/100)*
          pd.x == pd.x - pixels
          pixels == (pd.y/100)*
          pd.y == pd.y - pixels
    If someone sees an error feel free to correct :-P

  6. #6
    train spotter
    Join Date
    Aug 2001
    near a computer
    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)

    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; iImageHeight)/2;
    Rect.right= Rect.left +iImageWidth;
    //convert to DC scale
    DPtoLP (hdc, &Rect, 2) ;
    //draw the rect to show image size
    hBrush=CreateSolidBrush(RGB(0,0,0));//black brush
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7

    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


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. virtual memory
    By sweets in forum C Programming
    Replies: 6
    Last Post: 11-06-2004, 06:55 AM
  4. C Graphics - Page Flipping
    By z0diac in forum C Programming
    Replies: 1
    Last Post: 10-29-2002, 01:21 AM