Click here to Skip to main content
15,905,427 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 428.2K   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

 
AnswerRe: Can not allocate enough memory Pin
Milkacak8-Apr-03 0:55
Milkacak8-Apr-03 0:55 
GeneralAccess Violation Pin
6-Nov-02 23:40
suss6-Nov-02 23:40 
GeneralImage quality on 16-bit color display Pin
Subrahmanyam29-Oct-02 20:01
Subrahmanyam29-Oct-02 20:01 
GeneralSuggestion: Use Peter Hendrix' CPicture class instead Pin
W2k21-Sep-02 2:50
W2k21-Sep-02 2:50 
GeneralRe: Suggestion: Use Peter Hendrix' CPicture class instead Pin
W2k26-Sep-02 9:57
W2k26-Sep-02 9:57 
QuestionA little bug? Pin
eslea13-Aug-02 17:38
eslea13-Aug-02 17:38 
AnswerRe: A little bug? Pin
Yovav Gad13-Aug-02 17:48
sussYovav Gad13-Aug-02 17:48 
AnswerRe: A little bug? Pin
westwoodly21-Jul-03 3:12
westwoodly21-Jul-03 3:12 
At first ,my program is written in Debug version,your program is Release version.If I switch your program into Debug version,your program can bulid,link,but cannot exe.I added your some codes into my program from your program,the following errors appeared:

Debug Assertion Failed
program:G\my project\WorkTemplate\Debug\WorkTemplate.exe
File:objcore.cpp
Line:43

For infomation on how your program can cause an assertion failure,
see the the Visual C++ documentation on asserts.
(Press Retry to debug the application)

here are Abort Retry Ignore three buttons
If I click the Retry,the following words appeared:
unknown software exception(0x80000003),lies at 0x00498a92
GeneralRe: A little bug? Pin
Yovav21-Jul-03 3:29
Yovav21-Jul-03 3:29 
GeneralGetPixel Pin
afjimene14-Jun-02 10:44
afjimene14-Jun-02 10:44 
GeneralThe program crashed Pin
11-Jun-02 18:26
suss11-Jun-02 18:26 
GeneralRe: The program crashed Pin
11-Jun-02 18:28
suss11-Jun-02 18:28 
GeneralRe: The program crashed Pin
11-Jun-02 19:54
suss11-Jun-02 19:54 
GeneralRe: The program crashed Pin
Fanwy26-Jun-03 17:09
Fanwy26-Jun-03 17:09 
GeneralImages Pin
5-Jun-02 7:38
suss5-Jun-02 7:38 
GeneralRe: Images Pin
Yovav7-Jun-02 12:27
Yovav7-Jun-02 12:27 
GeneralDisplaying Images 2 Pin
31-May-02 8:25
suss31-May-02 8:25 
GeneralRe: Displaying Images 2 Pin
Yovav31-May-02 9:25
Yovav31-May-02 9:25 
GeneralDisplaying image Pin
30-May-02 14:14
suss30-May-02 14:14 
GeneralRe: Displaying image Pin
Yovav31-May-02 2:15
Yovav31-May-02 2:15 
GeneralIPicture will change the width and the height of a picture loaded! Pin
15-May-02 17:10
suss15-May-02 17:10 
GeneralRe: IPicture will change the width and the height of a picture loaded! Pin
Yovav17-May-02 5:16
Yovav17-May-02 5:16 
GeneralIPicture can't use in the map mode of HIMETRIC or more Pin
15-May-02 17:05
suss15-May-02 17:05 
Generalbitmap from file... Pin
yair241-May-02 4:10
yair241-May-02 4:10 
GeneralRe: bitmap from file... Pin
1-May-02 5:40
suss1-May-02 5:40 

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.