Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / MFC
Article

WndImage Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (17 votes)
6 May 2002CPOL 121.7K   6.2K   79   15
An easy-to-use control to display bitmaps (stretch, scale, tile)

Sample Image - WndImg.jpg

CWndImage Control

CWndImage can display a Windows Bitmap at arbitrary scales. It provides default scale and alignment modes for easy use, while free scaling and shifting, and a tile mode, is also available. This control can be handy if you have problems with the size of bitmaps in dialogs at different systems.

No image processing, rotating, shearing available or intended.

Overview

  • Easy use: put a static control on a dialog resource, then call CWndImage::CreateFromStatic, to replace it with the WndImage control
  • SetImg: HBITMAP, Resource ID, or SetImgFile(filename)
  • SetBltMode for predefined scaling modes
  • SetAlign for predefined alignment (left/center/right, top/center/bottom)
  • SetZoom and SetOrigin for custom placement
  • SetSourceRect for displaying only a clipped rectangle of the bitmap

Try it!

  1. add the WndImage.h and wndImage.cpp to your project, #include wndimage.h where you need it
  2. add a static or "picture" control to one of your dialogs. Give it an IDC_ST_IMG control ID.
  3. Add a CWndImage m_img member variable to your dialog class
  4. Add the following lines to OnInitDialog:
  5. m_img.CreateFromStatic( GetDlgItem(IDC_ST_IMG) );
    m_img.SetImg(IDB_MYBITMAP);  // specify a valid bitmap resource here
    m_img.SetBltMode(CWndImage::bltFitXY);

(if you used a picture control, and already specified a bitmap, you can even omit the second and third line)

Documentation

Create

BOOL Create(RECT const & r, CWnd * parent, UINT id, 
            DWORD dwStyle = WS_CHILD | WS_VISIBLE)

BOOL CreateFromStatic(CWnd * static)<br>

CreateFromStatic will use a static control's style, position, and ID, and replace it with the wndImage control. If a bitmap was specified for the static control (you nedd to choose 'picture control' in the resource editor for this), this bitmap is displayed in FitXY mode (stretched proportional to fit without clipping).
Not that only window-specific styles (like WS_BORDER) will be visible with the control. Window styles specific to static controls have no effect

Select Image

void SetImg(HBITMAP bmp, bool shared = false)
void SetImg(CBitmap * bmp)

bool SetImg(LPCTSTR resID, HINSTANCE instance = 0)   // Load from resource<br>
bool SetImg(UINT resID, HINSTANCE instance = 0)      // Load from resource<br>
bool SetImgFile(LPCTSTR fileName)                    // Load from File<br>
<br>
shared: if true, the WndImage control will not DeleteObject() the HBITMAP provided upon destruction. The prototypes 2..5 do not share the bitmap.
instance: if not NULL, specifies the module handle from which to load the bitmap

Standard Blit Modes

void SetBltMode(int mode)

bltNormal the image is drawn in it's original size a/r
bltStretch the image is stretched to fit the entire window
bltFitX the image is scaled proportional, to horizontally fit the window a/r
bltFitY the image is scaled proportional, to vertically fit the window a/r
bltFitXY the image is scaled proportional to the largest possible size that does not clip the image a/r
bltFitSm the image is scaled proportional so that one coordinate fits the window and the other is clipped
(this mode is good for small views whose aspect ratio differs strongly from the image's a/r)
a/r
bltTile the image is repeated in it's original size to fit the window a/r

Note: the constants must be prefixed with "CWndImage::"
Note: a/r: These modes preserve the image's aspect ratio

Standard Alignment

void SetAlign(int alignX, int alignY)<br>
    void SetAlignX(int alignX)<br>
    void SetAlignY(int alignY)

Valid alignments are: bltLeft, bltTop, bltCenter, bltRight, bltBottom (I hope you can figure out their meaning)

Custom Zoom

void SetZoom(double zoomX, double zoomY)<br>
    void SetZoom(double zoom)<br>
    <br>

Sets a zoom factor (1.0 == original size, <1 smaller, >1 larger). The second prototypwe sets a proportional zoom with zoomX == zoomY
Custom Zoom cannot be used together with bltTile mode.

Custom Alignment

void SetOrigin(int origX, int origY)<br>
    void SetOriginX(int origX)<br>
    void SetOriginY(int origY)<br>
    <br>

Sets the position of the images left upper corner in the window. positive values move the image to the center, negative values move it out of sight.
Custom alignment can be used with tiling, too

Source Clipping

void SetSourceRect(RECT const & r)<br>
    void SetSourceRect()<br>
    <br>

by using SetSourceRect, you can specify a clipping rectangle for the bitmap. This can be used with all modes. The coordinates are in bitmap pixels. The secnod prototype restores the setting to the entire bitmap.
When the image is changed (SetImg, SetImgFile), all settings except the Source window are preserved. Source window is reset to the entire image.

Background

void SetBackgroundBrush(HBRUSH)<br>
    void SetBackgroundBrush(int sysColorIndex)<br>
    void SetBackgroundBrush(CBrush & brush)<br>
    <br>

Sets the brush used to fill background not occupied by the image. For valid sysColorindex values, see GetSysColor Win32 API documentation (e.g. COLOR_WINDOW for default window background color, or COLOR_3DFACE for default dialog background)
Initial color is COLOR_3DFACE

Get Info

int GetImgSizeX() const int GetImgSizeY() const
int GetBltMode() const
double GetZoomX() const double GetZoomY() const
int GetAlignX() const int GetAlignY() const
int GetOriginX() const int GetOriginY() const

These functions return what they promise. The values are correct even in standard blit or alignment modes.

HBITMAP GetBitmap(bool detach = false)

Returns a handle the bitmap in use. If detach is true, the control will no longer use it.

Revision History:

Version 1.1 CreateFromStatic now uses the initial bitmap (if any).

License

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


Written By
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.


Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

Comments and Discussions

 
GeneralError!! Pin
zakaller16-Nov-08 22:43
zakaller16-Nov-08 22:43 
QuestionAbout viewing image files in databases Pin
Timothy Ayodele25-Oct-07 12:19
Timothy Ayodele25-Oct-07 12:19 
AnswerRe: About viewing image files in databases Pin
qiuxiangyong26-Aug-10 16:09
qiuxiangyong26-Aug-10 16:09 
GeneralNew version Pin
peterchen19-Apr-04 10:19
peterchen19-Apr-04 10:19 
GeneralRe: My kid sister? Pin
peterchen21-Jun-04 1:05
peterchen21-Jun-04 1:05 
QuestionExcellent !! What about flip,rotate and other functions? Pin
hvibh29-Nov-02 22:02
hvibh29-Nov-02 22:02 
your code is Excellent.
But what about the functions like flip, rotate, invert,black and white, change hue etc.
Can we do this by extending the class?


vibhavari

hvibh
AnswerRe: Excellent !! What about flip,rotate and other functions? Pin
namezero11111127-Mar-07 3:07
namezero11111127-Mar-07 3:07 
GeneralGood picture ! Pin
viton11-Nov-02 12:37
viton11-Nov-02 12:37 
GeneralRe: Good picture ! Pin
namezero11111127-Mar-07 3:10
namezero11111127-Mar-07 3:10 
Generalcopying a portion of the entire image Pin
sanskypotov14-Jun-02 4:10
sanskypotov14-Jun-02 4:10 
GeneralRe: copying a portion of the entire image Pin
peterchen14-Jun-02 5:14
peterchen14-Jun-02 5:14 
GeneralThanks, just what I needed... (n/t) Pin
Brendan Tregear12-Jun-01 20:20
Brendan Tregear12-Jun-01 20:20 
Generalnew version: 256 colors Pin
Peter hauptmann30-May-00 1:22
sussPeter hauptmann30-May-00 1:22 
GeneralPicture transparency Pin
Christian Skovdal Andersen17-May-00 5:32
Christian Skovdal Andersen17-May-00 5:32 
General256 colors support Pin
Yury Goltsman17-May-00 2:55
Yury Goltsman17-May-00 2:55 

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.