Click here to Skip to main content
11,643,493 members (74,821 online)
Click here to Skip to main content

PreviewCtrl

, 2 Sep 2005 72.1K 3.3K 52
Rate this:
Please Sign up or sign in to vote.
Showing an image in a CStatic control.

Sample Image - PreviewCtrl.jpg

Introduction

Drawing a CBitmap into a CStatic is a piece of cake. At least if you stop to mess around with CStatic::SetBitmap(). To get some running code, I derived CPreviewRect from CStatic and implemented the display routines manually. This small application demonstrates its usage.

Features

  • The shown images are resizable and always shown in correct aspect ratio.
  • The shown images can be resized to thumbnail size to save up memory and to speed up drawing.
  • The shown images can be specified by:
    • a path name
    • a resource ID
    • another CBitmap
    • raw RGB data (in conjunction with width and height information)
  • Compared to CStatic::SetBitmap() it works.

Background

CStatic::SetBitmap() drove me mad.

Using the code

Guess you have a dialog with a CStatic control on it, and you want to show loaded bitmaps in this control. First of all please make sure that the control's name differs from IDC_STATIC and create a related member variable. Now modify your code as follows:

In your dialog's header file:

// change the control's class from CStatic to CPreviewRect
CPreviewRect m_bitmap;

In your FileOpen handler:

// create your file dialog
CFileDialog dlg(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, 
                    "Bitmaps (.bmp)|*.bmp|All files(*.*)|*.*||");

// ask user
if (dlg.DoModal() == IDCANCEL) return;

// load the selected bitmap file
m_bitmap.LoadBitmap(dlg.GetPathName());

// check recent file operation (IsInitialized() should return TRUE)
if (!m_bitmap.IsInitialized())
{
    // load error bitmap from a resource (if you have any)
    m_bitmap.LoadBitmap(IDB_BLUESCREEN);
}

// resize bitmap to save up memory and speed up drawing
m_bitmap.CreateThumbnail(320, 240);

That's it!

History

Version 1.1

Added:

  • BorrowBitmap(CBitmap* pBitmap)

Changed:

  • CreateBitmap(CBitmap* pBitmap) has been renamed to CopyBitmap(CBitmap* pBitmap)

Version 1.0

Added:

  • LoadBitmap(const CString& Path)
  • LoadBitmap(UINT nIDResource)
  • CreateBitmap(CBitmap* pBitmap)
  • CreateBitmap(int Width, int Height, unsigned char* BGRA)
  • CreateThumbnail(int MaxWidth, int MaxHeight)
  • Reset(BOOL GraphicalUpdate = TRUE)

License

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

Share

About the Author

Achim Klein
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralOnly horizontal resizing. . Pin
Varchas R S27-Nov-08 23:31
memberVarchas R S27-Nov-08 23:31 
General[Message Removed] Pin
immetoz4-Oct-08 2:01
memberimmetoz4-Oct-08 2:01 
GeneralThanks a lot! Pin
rehanone17-Jul-08 3:12
memberrehanone17-Jul-08 3:12 
Generalthanks !!! Pin
jofavi7-Apr-07 13:57
memberjofavi7-Apr-07 13:57 
GeneralWonderful Pin
del Fuego2-Feb-07 6:31
memberdel Fuego2-Feb-07 6:31 
GeneralSuperb Pin
Expategghead6-Mar-06 0:15
memberExpategghead6-Mar-06 0:15 
GeneralThenk you, Pin
Staon15-Jan-06 22:33
memberStaon15-Jan-06 22:33 
QuestionHow to save CBitmap to file Pin
caykahve24-Dec-05 9:39
membercaykahve24-Dec-05 9:39 
QuestionWay cool error message/bmp Pin
radeutsch7-Sep-05 2:07
memberradeutsch7-Sep-05 2:07 
AnswerRe: Way cool error message/bmp Pin
Achim Klein8-Sep-05 0:47
memberAchim Klein8-Sep-05 0:47 
GeneralDOxygen Pin
thudriact2-Sep-05 2:08
memberthudriact2-Sep-05 2:08 
GeneralRe: DOxygen Pin
Achim Klein8-Sep-05 1:27
memberAchim Klein8-Sep-05 1:27 
GeneralIt's simple Pin
CandleLight6662-Sep-05 0:58
memberCandleLight6662-Sep-05 0:58 
GeneralThank you! Pin
Michael Kane2-Sep-05 0:49
memberMichael Kane2-Sep-05 0:49 

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 | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 2 Sep 2005
Article Copyright 2005 by Achim Klein
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid