Click here to Skip to main content
15,885,953 members
Articles / Desktop Programming / MFC

CPicture - The Yovav (Horror) PictureShow

Rate me:
Please Sign up or sign in to vote.
4.96/5 (34 votes)
18 Jun 2003CPOL 425.7K   10.8K   105   120
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.

COPYFREE (F) - ALL RIGHTS FREE

class CPicture
{
public:
	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);

	CPicture();
	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 

(.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// 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);
//

License

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


Written By
President NEW media
United States United States
Creator of NEW media

Comments and Discussions

 
GeneralUsing the Show Function not in OnPaint Pin
Jens Lengwenus18-May-04 1:32
Jens Lengwenus18-May-04 1:32 
GeneralRe: Using the Show Function not in OnPaint Pin
Jens Lengwenus18-May-04 1:51
Jens Lengwenus18-May-04 1:51 
GeneralThank u for your great article! Pin
eslea26-Apr-04 18:44
eslea26-Apr-04 18:44 
GeneralSample Demo Pin
Member 80334811-Jan-04 8:18
Member 80334811-Jan-04 8:18 
GeneralRe: Sample Demo Pin
Yovav11-Jan-04 20:11
Yovav11-Jan-04 20:11 
GeneralNice to See CPicture still in use Pin
Anonymous23-Dec-03 19:54
Anonymous23-Dec-03 19:54 
Generaldetaching a shown image Pin
satadru9-Dec-03 0:42
satadru9-Dec-03 0:42 
GeneralRe: detaching a shown image Pin
Yovav9-Dec-03 4:28
Yovav9-Dec-03 4:28 
Check on MSDN: Invalidate() and InvalidateRect(...)

Best Regards - Yovav Gad
EMail: Dev@GadWorks.com
Web-Site: www.GadWorks.com
QuestionHow can I save a gif as a 24 bit bitmap? Pin
annephilip3-Oct-03 7:36
annephilip3-Oct-03 7:36 
AnswerRe: How can I save a gif as a 24 bit bitmap? Pin
Yovav14-Oct-03 23:33
Yovav14-Oct-03 23:33 
GeneralActiveX control wanted Pin
Xilin26-Aug-03 22:53
Xilin26-Aug-03 22:53 
GeneralRe: ActiveX control wanted Pin
Yovav26-Aug-03 23:28
Yovav26-Aug-03 23:28 
QuestionWhen is the picture updated ? Pin
Oliver, an eager novice11-Aug-03 1:51
sussOliver, an eager novice11-Aug-03 1:51 
AnswerThe picture is updated ! Pin
Oliver, now wiser.12-Aug-03 21:50
sussOliver, now wiser.12-Aug-03 21:50 
QuestionHow do I....... Pin
Osrald29-Jul-03 20:03
Osrald29-Jul-03 20:03 
GeneralThis is what we call: CPicture Pin
Anonymous20-Jun-03 8:06
Anonymous20-Jun-03 8:06 
GeneralRe: This is what we call: CPicture Pin
Yovav20-Jun-03 12:54
Yovav20-Jun-03 12:54 
GeneralRe: I am NOT Paul. Pin
Anonymous25-Jun-03 8:14
Anonymous25-Jun-03 8:14 
GeneralRe: I am NOT Paul. Pin
Yovav25-Jun-03 9:10
Yovav25-Jun-03 9:10 
Generaldisplay gif image on dialog Pin
jitwadee26-May-03 19:25
jitwadee26-May-03 19:25 
GeneralRe: display gif image on dialog Pin
Anonymous26-May-03 20:13
Anonymous26-May-03 20:13 
GeneralRe: display gif image on dialog Pin
Yovav26-May-03 20:16
Yovav26-May-03 20:16 
AnswerRe: Use in OnPaint() function.. Pin
hyungju kim10-Oct-06 20:16
hyungju kim10-Oct-06 20:16 
GeneralScroll bars Pin
Milkacak7-Apr-03 21:35
Milkacak7-Apr-03 21:35 
General[Wow!!] Pin
umito12-Feb-03 19:53
umito12-Feb-03 19:53 

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.