CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Graphics & Multimedia >> Bitmaps & Palettes


How to Retain the Aspect Ratio of an Image While Resizing
Rating: none

Michael Hatton (view profile)
January 3, 2002


(continued)



Environment: VC6

I needed a way to display images full screen but I did not want the image to change its aspect ratio since that would distort its shape. If the image was very narrow it would expand to fill up the whole width of the screen making the image look too wide.

To fix this I wrote a function that will calculate the rectangle that will be needed. If you stretch the image to fit in the rectangle it will expand the image to fill as much of the screen as possible without changing the aspect ratio of the picture.

The function has three parameters. First is the rectangle that you want the image to fill. The second is the size of the picture. The last parameter tells the function whether you want the image to be centered in the rectangle or not.

The function returns a rectangle that you use to stretch the image.

CRect SizeRectWithConstantAspectRatio( CRect* rcScreen,
                                       CSize sizePicture,
                                       BOOL bCenter)
{
  CRect rect(rcScreen);
  double dWidth = rcScreen->Width();
  double dHeight = rcScreen->Height();
  double dAspectRatio = dWidth/dHeight;

  double dPictureWidth = sizePicture.cx;
  double dPictureHeight = sizePicture.cy;
  double dPictureAspectRatio = dPictureWidth/dPictureHeight;

  //If the aspect ratios are the same then the screen rectangle
  // will do, otherwise we need to calculate the new rectangle

  if (dPictureAspectRatio > dAspectRatio)
  {
    int nNewHeight = (int)(dWidth/dPictureWidth*dPictureHeight);
    int nCenteringFactor = (rcScreen->Height() - nNewHeight) / 2;
    rect.SetRect( 0,
                  nCenteringFactor,
                  (int)dWidth,
                  nNewHeight + nCenteringFactor);

  }
  else if (dPictureAspectRatio < dAspectRatio)
  {
    int nNewWidth =  (int)(dHeight/dPictureHeight*dPictureWidth);
    int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;
    rect.SetRect( nCenteringFactor,
                  0,
                  nNewWidth + nCenteringFactor,
                  (int)(dHeight));
  }

  return rect;
}

This is an example of how to use this function. I used the CPicture class from CodeGuru but you could use a normal CBitmap class and use CDC::StretchBlt for the same effect.

void CMyWnd::OnPaint()
{
  CPaintDC dc(this); // device context for painting
  CString strSize;

  CPicture picture;
  picture.Load(m_strPicturePath);
  // Get Picture Dimentions In Pixels
  picture.UpdateSizeOnDC(&dc);

//Get the dimensions of the screen
  CRect rcScreen( 0, 0,
                  GetSystemMetrics(SM_CXSCREEN),
                  GetSystemMetrics(SM_CYSCREEN));

  //Get dimensions of Image
  CSize sizePicture(picture.m_Width, picture.m_Height);

  //Create a black background
  CBrush backBrush;
  backBrush.CreateSolidBrush(RGB(0,0,0));
  dc.FillRect(&rcScreen, &backBrush);

  CRect rcNewPictureRect =
        SizeRectWithConstantAspectRatio(&rcScreen,
                                        sizePicture,
                                        TRUE);
  picture.Show(&dc, rcNewPictureRect);
}

Downloads

Download demo project - 29 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
An other way to do this... - Legacy CodeGuru (09/15/2003)
Helpful - Legacy CodeGuru (06/26/2002)
algorithm looks good, but would like more formatting in code - Legacy CodeGuru (01/02/2002)
Works Fine - Legacy CodeGuru (12/23/2001)
Centering - Legacy CodeGuru (12/21/2001)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES