Save My Job! Resizing Bitmaps to a Client Window

Dear Dr. GUI:

Help me, Dear Doctor! You are my last hope. My boss has been hounding me to get this done as soon as possible or face imminent dismissal from my project! I am trying to simply fit a bitmap to the client area of a window without too much distortion. Seems pretty trivial, but I just can't figure it out. Everything I have tried looks really bad.

Wendy Reynolds
Gig Harbor, Washington

Dr. GUI replies:

<Music> Here I am to save the day...Dr. GUI's on the way <end music>. Don't fear, Wendy, I have exactly what you need. The key to the solution is to maintain the ratio between the original dimensions of the bitmap and the subsequent dimensions of the stretched bitmap, that is, x/y = xnew/ynew. The following code calculates the new x and y dimensions of the bitmap. The xDest and yDest parameters are the extents of the destination rectangle. The xBmp and yBmp parameters are the extents of the original bitmap. The new dimensions are written to the xNew and yNew parameters (pointers to integers).

BOOL WINAPI FitToScreen( int xDest, int yDest, int xBmp, int yBmp, int * xNew, 
   int * yNew )
{

   int xDiff, yDiff, xAbs, yAbs, xAdj, yAdj, xWidth, yWidth;

   xDiff = xDest - xBmp;
   yDiff = yDest - yBmp;
   // Negative values for xDiff and yDiff imply that the bitmap
   // is larger than the dimensions of the destination retangle.

   xAbs = abs ( xDiff );
   yAbs = abs ( yDiff );

   if ( xAbs > yAbs )
   {
      xAdj = MulDiv ( xBmp, yDiff, yBmp );
      xWidth = xBmp + xAdj;
      yWidth = yBmp + yDiff;
   }
   else if ( xAbs < yAbs )
   {
      yAdj = MulDiv ( yBmp, xDiff, xBmp );
      xWidth = xBmp + xDiff;
      yWidth = yBmp + yAdj;
   }
   else   // they are the same
   {
      xWidth = xBmp + xDiff;
      yWidth = yBmp + yDiff;
   }
   
   * xNew = xWidth;
   * yNew = yWidth;

   return ( TRUE );
}