Click here to Skip to main content
15,125,746 members
Articles / Desktop Programming / MFC
Posted 3 Oct 2001


104 bookmarked

CPicture - The Yovav (Horror) PictureShow

Rate me:
Please Sign up or sign in to vote.
4.96/5 (34 votes)
18 Jun 2003CPOL
Routines for displaying image files (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)

After many days of searching (and not finding) a way to load a JPG from a resource and show it on a dialog based application, I decided to take steps

So I created what I call a very simple and useful class, it can easily be implemented by adding it to a project, and you do not have to be a real JPEG freak and invent all header reading from the beginning (it uses the IPicture interface - same way as Internet Explorer does)

About The Project

I was little carried away with this "ACDSee alike" picture viewer, as it was not my main purpose - I did not have the time to make it "perfect", if you feel lucky and want to improve it here and there then please share it with me.


class CPicture
	void FreePictureData();
	BOOL Load(CString sFilePathName);
	BOOL Load(UINT ResourceName, LPCSTR ResourceType);
	BOOL LoadPictureData(BYTE* pBuffer, int nSize);
	BOOL SaveAsBitmap(CString sFilePathName);
	BOOL Show(CDC* pDC, CPoint LeftTop, CPoint WidthHeight, int MagnifyX,
                  int MagnifyY);
	BOOL Show(CDC* pDC, CRect DrawRect);
	BOOL ShowBitmapResource(CDC* pDC, const int BMPResource, 
                                CPoint LeftTop);
	BOOL UpdateSizeOnDC(CDC* pDC);

	virtual ~CPicture();
        // Same As LPPICTURE (typedef IPicture __RPC_FAR *LPPICTURE)
	IPicture* m_IPicture; 
	// Height (In Pixels Ignor What Current Device Context Uses)
	LONG m_Height; 	     
        // Size Of The Image Object In Bytes (File OR Resource) 
        LONG m_Weight;	
        // Width (In Pixels Ignor What Current Device Context Uses)
         LONG m_Width;  
//~~~~~~~~~~~~~~~~~~~~~~~~~~Example & Usage 4 Dummies~~~~~~~~~~~~~~~~~~~~~~~~
//  U Need 2 Add "CPicture.CPP" and "CPicture.H" Into Your Project 
// (From FileView)
//  So U Will Get Control Over The Functions In This Class,
//  Then U Can Create a Picture Object And Show It On a Device Context
// Create a Picture Object (An Instance Of This Class)
//  CPicture m_Picture;  

// Make Sure U Include This Where U Gonna Create The Object...
//  #include "Picture.h" 
//  Load Picture Data Into The IPicture Interface 

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Load From a File - Just Load It (Show Later)
//	m_Picture.Load("Test.JPG"); 
// Load From a Resource - Just Load It (Show Later)
//	m_Picture.Load(IDR_TEST, "JPG"); 

//  (U Must Include IDR_TEST In Your Resources Under a Custom Name, 4 
//   Example - "JPG")
//  When Using DC Object On a *Dialog Based* Application (CPaintDC dc(this);)
//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get Picture Dimentions In Pixels
//      m_Picture.UpdateSizeOnDC(&dc); 
//	m_Picture.Show(&dc, CPoint(0,0), 
                       CPoint(m_Picture.m_Width, m_Picture.m_Height), 0,0);
//      Change Original Dimentions
//      m_Picture.Show(&dc, CRect(0,0,100,100)); 

//      Show Bitmap Resource
//	m_Picture.ShowBitmapResource(&dc, IDB_TEST, CPoint(0,0)); 
//  OR When Using a Pointer On a "Regular" MFC Application (CDC* pDC)
//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	m_Picture.UpdateSizeOnDC(pDC); // Get Picture Dimentions In Pixels
//	m_Picture.Show(pDC, CPoint(0,0),  CPoint
//                     m_Picture.m_Width,m_Picture.m_Height), 0,0);
//      Change Original Dimentions
//	m_Picture.Show(pDC, CRect(0,0,100,100)); 
//      Show Bitmap Resource
//	m_Picture.ShowBitmapResource(pDC, IDB_TEST, CPoint(0,0)); 
//  Show Picture Information
//  ~~~~~~~~~~~~~~~~~~~~~~~~
//	CString S;
//	S.Format("Size = %4d\nWidth = %4d\nHeight = %4d\nWeight = %4d\n",
//	         m_Picture.m_Weight, m_Picture.m_Width, 
//               m_Picture.m_Height,  m_Picture.m_Weight);
//	AfxMessageBox(S);


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


About the Author

President NEW media
United States United States
Creator of NEW media

Comments and Discussions

GeneralMy vote of 4 Pin
galuo14-Sep-13 1:21
Membergaluo14-Sep-13 1:21 
Questioncan any body encapsulates all this heap of code in one class usable with dialogbox control Pin
blackhack500015-Nov-10 20:05
Memberblackhack500015-Nov-10 20:05 
Generalimage in case of dialog box apps Pin
Sadru1-Mar-09 21:29
MemberSadru1-Mar-09 21:29 
QuestionHow to rotate ? Pin
hujunxi8-May-08 17:04
Memberhujunxi8-May-08 17:04 
GeneralJust one word Pin
Demian Panello7-Jun-07 16:17
MemberDemian Panello7-Jun-07 16:17 
GeneralThanks Pin
Kaworu221-Mar-06 23:34
MemberKaworu221-Mar-06 23:34 
QuestionHow do i use it for IDC_STATIC control? Pin
etboite10-Nov-05 23:05
Memberetboite10-Nov-05 23:05 
AnswerRe: How do i use it for IDC_STATIC control? Pin
KaЯl23-Feb-06 21:56
MemberKaЯl23-Feb-06 21:56 
AnswerRe: How do i use it for IDC_STATIC control? Pin
serup18-Sep-08 4:52
Memberserup18-Sep-08 4:52 
GeneralDisplaying big images (need ScrollBar) Pin
angel42018-Aug-05 14:27
Memberangel42018-Aug-05 14:27 
QuestionPixel value ? Pin
angel42017-Aug-05 14:42
Memberangel42017-Aug-05 14:42 
AnswerRe: Pixel value ? Pin
Christian Graus17-Aug-05 15:39
protectorChristian Graus17-Aug-05 15:39 
GeneralRe: Pixel value ? Pin
angel42018-Aug-05 14:01
Memberangel42018-Aug-05 14:01 
GeneralNice Class Pin
ThatsAlok8-Jun-05 20:05
MemberThatsAlok8-Jun-05 20:05 
GeneralCPicture to HBITMAP Pin
naragana23-May-05 16:36
Membernaragana23-May-05 16:36 
GeneralRe: CPicture to HBITMAP Pin
ThatsAlok20-Jun-05 1:46
MemberThatsAlok20-Jun-05 1:46 
Generalbug in CPictureShowDoc::CreateFilesList Pin
Shay cohen4-Apr-05 23:22
MemberShay cohen4-Apr-05 23:22 
GeneralHelp!!!!! Pin
subbuviv31-Mar-05 0:24
Membersubbuviv31-Mar-05 0:24 
Generalchange pictures in Dialog Pin
olis7-Feb-05 5:31
Memberolis7-Feb-05 5:31 
GeneralRotating an Image Pin
Alex Evans3-Jan-05 17:42
MemberAlex Evans3-Jan-05 17:42 
GeneralRe: Rotating an Image Pin
Fransiscusherry26-Sep-05 0:53
MemberFransiscusherry26-Sep-05 0:53 
GeneralLoading a picture from an ftp site Pin
Member 5343573-Sep-04 4:48
MemberMember 5343573-Sep-04 4:48 
Generalproblem in image compression in database Pin
MemberKRANTHI KUMAR KUKKALA31-Aug-04 4:45 
Questionhow to get image data as pixels? Pin
sgorgulu23-Aug-04 4:15
Membersgorgulu23-Aug-04 4:15 
Generalhelloo sir one problem Pin
ThatsAlok17-Jul-04 1:52
MemberThatsAlok17-Jul-04 1:52 

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.