Click here to Skip to main content
Click here to Skip to main content
Go to top

WndImage Control

, 6 May 2002
Rate this:
Please Sign up or sign in to vote.
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)

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

bool SetImg(UINT resID, HINSTANCE instance = 0)      // Load from resource

bool SetImgFile(LPCTSTR fileName)                    // Load from File


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)

    void SetAlignX(int alignX)

    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)

    void SetZoom(double zoom)

    

    

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)

    void SetOriginX(int origX)

    void SetOriginY(int origY)

    

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)

    void SetSourceRect()

    

    

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)

    void SetBackgroundBrush(int sysColorIndex)

    void SetBackgroundBrush(CBrush & brush)

    

    

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)

Share

About the Author

peterchen
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

 
QuestionAbout viewing image files in databases PinmemberTimothy Ayodele25-Oct-07 12:19 
AnswerRe: About viewing image files in databases Pinmemberqiuxiangyong26-Aug-10 16:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 May 2002
Article Copyright 2000 by peterchen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid