Click here to Skip to main content
11,928,754 members (53,315 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked

An image preview dialog for adjusting brightness and contrast

, 30 Apr 2003
Rate this:
Please Sign up or sign in to vote.
An image preview dialog for adjusting brightness and contrast

Sample Image - ImagePreview.jpg


For one of my projects, I need to show an image preview dialog while adjusting brightness and contrast. Here is a sample of what I have created. It doesn't rely on any other graphics library. No GDI+ needed.

Why do it?

Sometimes refreshing a big image file takes a lot of RAM and processor power. A more efficient way is to take a sample from the complete image and show the effect in a preview dialog.

In my example, I am loading the bitmap from a resource file. However, you can easily load a 100x100 sample from your full image to show your user, the effect of your adjustment. I have seen programs that implement a caret box that let you drag around the full image to take such sample.

It is usually not a good idea to resample/resize the image into a 100x100 bitmap and put it into the preview window. The user won't be able to see clearly the final effect.


Only 2 files in the sample project are important:

  • DialogBrightnessContrast.h
  • DialogBrightnessContrast.cpp

You should look at the BrightnessAndContrast() function. It loops through every pixel in the bitmap and changes its RGB value.

for (m_nY = 0; m_nY < m_bmp.bmHeight; m_nY++) {
     for (m_nX = 0; m_nX < m_bmp.bmWidth; m_nX++) {
         m_crC = m_dcSource.GetPixel(m_nX, m_nY);

         m_nC = GetRValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nR = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_nC = GetGValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nG = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_nC = GetBValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nB = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_crC = m_nR + (m_nG << 8) + (m_nB << 16);
         m_dcOffScreen.SetPixel(m_nX, m_nY, m_crC);

To adjust brightness you are increasing all the RGB values of a color. To adjust contrast you are exaggerating the difference of a RGB value from GREY(128, 128, 128).

If you have library that does invert, tint and do other graphic manipulations, you can use them here and modify the preview image.

Final words

This is not the most efficient way to apply filter to your whole image. However it is sufficient for the preview dialog. When you hit OK button, you should pass the brightness and contrast values to your super JPG, GIF, PNG engine to crank out the best optimized photo.

May the code be with you.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Frank is a Sr. UI Software Engineer specialized in MFC and Business Applications.

He has worked in BROADBASE, KANA, Macromeda and Siebel Systems.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 101009489-Jun-13 20:04
memberMember 101009489-Jun-13 20:04 
GeneralSuggestion for contrast Pin
SolarAngel22-Jun-08 4:50
memberSolarAngel22-Jun-08 4:50 
Generalmsg!!!! Pin
shikha_dawer16-Jan-05 22:03
membershikha_dawer16-Jan-05 22:03 
GeneralAbout Auto-Level and Auto-Contrast Pin
Hanney Wang31-Mar-04 16:29
memberHanney Wang31-Mar-04 16:29 
GeneralRe: About Auto-Level and Auto-Contrast Pin
Paul Kissel14-Sep-07 12:07
memberPaul Kissel14-Sep-07 12:07 
Generalfor jpeg Pin
henli6-Aug-03 16:06
memberhenli6-Aug-03 16:06 
GeneralRe: for jpeg Pin
jemodurn7-Aug-03 12:28
memberjemodurn7-Aug-03 12:28 
GeneralRe: for jpeg Pin
henli10-Aug-03 16:02
memberhenli10-Aug-03 16:02 
GeneralNot bad! Pin
WREY4-Jun-03 8:07
memberWREY4-Jun-03 8:07 
GeneralNice! Pin
Peter Mares2-May-03 0:21
memberPeter Mares2-May-03 0:21 
QuestionWhere's the article body? Pin
Johann Gerell1-May-03 20:30
memberJohann Gerell1-May-03 20:30 
AnswerRe: Where's the article body? Pin
jemodurn1-May-03 22:46
memberjemodurn1-May-03 22:46 

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
Web04 | 2.8.151126.1 | Last Updated 1 May 2003
Article Copyright 2003 by jemodurn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid