Click here to Skip to main content
12,689,794 members (22,881 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked

Using GDI+ in a Single Document MFC Project to Rotate, Zoom, and Constraint an Image

, 27 Aug 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Using the GDI+ library to do some tricky things


The program loads images (BMP, GIF, JPEG/JPG/EXIF, PNG, TIF, ICON, WMF, EMF), applies some constraints related to window size (best fit, constraint width, constraint height, image original), rotates the image, makes zoom in & out effect and finally navigates through multi-page TIF images. It is useful if you need a image viewer class and image functionality in your C++ application. The problem is that I use GDI+. Developing with GDI+, there is a point most of us must go through.

First, go beyond pointers to GDI+ Image class that block access to every image file I load and view; now only the current loaded is blocked. No more headache with blocked temp image files, we got the key. How to make a big JPEG/JPG/EXIF move fast? I made it, but unfortunately it won't work for a rotation with 180 degrees. Just stretch the image in a hidden window: rotate 90 degrees Graphics::RotateTransform(...) , and display image, don't ask why! It will work to your surprise. I found many programmers that rotated an image with Image class method RotateFlip (and I did it too), instead of Graphics class method RotateTransform. This implies that an image will have negative values along X, Y axis and more ... but will have intact integrity of its internal structure. What I didn't solve is transparent background images management, and what to do to cheat image at a 180 angle rotation.


C/C++ is great, but to know it from the very beginning.

Using the Code

The class that do everything that I discussed earlier is mvImage. So you will need this #include "mvImage.h" in your application. The mvImage class constructor needs a window handle HWND. The rest does the class through this handle. You can use this class in a single / multi document interface or dialog based MFC project. I will try a poor demonstration copying parts of the code here to show you: easier than this doesn't exist !

  1. Your CView derived class constructor will be like this:
    CDraw1View::CDraw1View() { m_Image = 0x000000 ; }
  • 2.1 Initialization of mvImage pointer comes in OnDraw once per application, the rest is redrawing the window surface.
    void CDraw1View::OnDraw(CDC* pDC)
        CDraw1Doc* pDoc = GetDocument(); 
        // TODO: add draw code for native data here 
        //init image pointer 
        if((m_Image == 0x000000) && (this->m_hWnd != 0x000000)) 
            m_Image = new mvImage(this->m_hWnd); 
            //redraw image window 
            if(m_Image->mvIsValidImage()) m_Image->mvOnPaint(); 
  • 2.2 If your application is dialog based, use OnInitDialog to put this code in:
    if((m_Image == 0x000000) && (this->m_hWnd != 0x000000))
        m_Image = new mvImage(this->m_hWnd);

    and OnPaint method with this code:

    if(m_Image->mvIsValidImage()) m_Image->mvOnPaint() ;
  1. To load and display an image with 'best fit' propriety, use this:
    CFileDialog Fdlg(TRUE) ;
    if(Fdlg.DoModal() == IDOK)
        CString path = Fdlg.GetPathName() ; 
        m_Image->mvLoadImage(path) ;
    else return ;
    //display image 'best fit' in wind.
    if(m_Image->mvIsValidImage()) m_Image->mvConstraintAll();
  2. Constraint all 'best fit' image in window, proceed like here:
  3. Constraint height ? like here:
        m_Image->mvConstraintHeight() ;
  4. Rotate left:
        m_Image->mvRotateLeft() ;
  5. Zoomin:
        m_Image->mvZoomIn() ;
  6. Entire vertical scroll function:
    void CDraw1View::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
        // TODO: Add your message handler code here and/or call default
            m_Image->mvOnVScroll(nSBCode, nPos, pScrollBar) ;
        CView::OnVScroll(nSBCode, nPos, pScrollBar);
  7. Route OnSize message like this:
    void CDraw1View::OnSize(UINT nType, int cx, int cy)
        CView::OnSize(nType, cx, cy);
        if((m_Image != 0x000000) && (this->m_hWnd != 0x000000))
                m_Image->mvOnSize(nType, cx, cy) ; 
  8. Multi-page TIFF navigation:

Searching through the source code in draw1View.cpp, you will find all the code you may be interested in.


  • 27th August, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

AnswerBad code Pin
htDestiny6-Jul-13 8:33
memberhtDestiny6-Jul-13 8:33 
GeneralRe: Bad code Pin
mihK8-Jul-13 23:20
membermihK8-Jul-13 23:20 
GeneralMy vote of 5 Pin
manoj kumar choubey18-Feb-12 4:28
membermanoj kumar choubey18-Feb-12 4:28 
Questionno source files?? Pin
dumnbncool27-May-09 7:13
memberdumnbncool27-May-09 7:13 
AnswerRe: no source files?? Pin
mihK27-May-09 8:37
membermihK27-May-09 8:37 
GeneralMemory Data Pin
heracles710_rns12-Oct-08 17:34
memberheracles710_rns12-Oct-08 17:34 
QuestionWhy can not be print? Pin
Mila17-Sep-08 22:37
memberMila17-Sep-08 22:37 
GeneralError Pin
Oonaa Lee25-Nov-07 16:16
memberOonaa Lee25-Nov-07 16:16 
GeneralGreat article... but need some details Pin
Kiran Satish18-Oct-07 11:13
memberKiran Satish18-Oct-07 11:13 
Generaltoo slow Pin
malfaro20-Sep-07 6:27
membermalfaro20-Sep-07 6:27 
GeneralAt last I make it. Pin
Chhoto Bou15-Jan-07 19:47
memberChhoto Bou15-Jan-07 19:47 
GeneralRuntime Error Solve. But .png File not shown in dialog. Pin
Chhoto Bou15-Jan-07 19:32
memberChhoto Bou15-Jan-07 19:32 
GeneralRe: Runtime Error Solve. But .png File not shown in dialog. Pin
mihK15-Jan-07 21:11
membermihK15-Jan-07 21:11 
GeneralRe: Runtime Error Solve. But .png File not shown in dialog. Pin
mihK15-Jan-07 21:22
membermihK15-Jan-07 21:22 
QuestionDialog based Example Pin
Chhoto Bou15-Jan-07 18:42
memberChhoto Bou15-Jan-07 18:42 
AnswerRe: Dialog based Example Pin
malfaro20-Sep-07 6:28
membermalfaro20-Sep-07 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 27 Aug 2006
Article Copyright 2006 by mihK
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid