Click here to Skip to main content
15,888,527 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 426K   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

 
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 
GeneralRe: bitmap from file... Pin
yair241-May-02 6:05
yair241-May-02 6:05 
GeneralError When Use CPicture Pin
21-Apr-02 19:20
suss21-Apr-02 19:20 
GeneralRe: Error When Use CPicture Pin
25-Apr-02 20:19
suss25-Apr-02 20:19 
I know there is a problem to pass compile on DEBUG mode
is this your problem ?

Try 2 replace this function in your CPictureShowDoc.cpp (CPictureShowDoc::CreateFilesList())
I think these two lines may fix it,

// 10X 2 Mr.Christian Wagner 4 His Help 2 Avoid An Assertion On Debug...
m_Finding = m_Finder.FindNextFile(); // Must Be Called Once Before IsDots()

Please let me know if it worked...

//-----------------------------------------------------------------------------
BOOL CPictureShowDoc::CreateFilesList()
//=============================================================================
{
m_Finding = m_Finder.FindFile("*.*"); // Finds All Files (By Name)

CString Extention;

m_FilesList.RemoveAll(); // Clear - In Case We Created Another List...
m_FilesList.GetHeadPosition();

while(m_Finding == TRUE)
{
// 10X 2 Mr.Christian Wagner 4 His Help 2 Avoid An Assertion On Debug...
m_Finding = m_Finder.FindNextFile(); // Must Be Called Once Before IsDots()

if(m_Finder.IsDots() || m_Finder.IsDirectory()) m_Finding = m_Finder.FindNextFile();
else // It Is a File...
{
// Get The Extention Of The Current Handled File
Extention.Format("%s", FileExtentionOnly(m_Finder.GetFileName()));
Extention.MakeUpper(); // Just In Case (Sensitive)...

// Add To The List
if(IsWantedExtention(Extention)) m_FilesList.AddTail(m_Finder.GetFileName());

m_Finding = m_Finder.FindNextFile();
}
}

// Handle The Last File...
Extention.Format("%s", FileExtentionOnly(m_Finder.GetFileName()));
Extention.MakeUpper(); // Just In Case (Sensitive)...

if(IsWantedExtention(Extention)) m_FilesList.AddTail(m_Finder.GetFileName());

m_Finder.Close();

// Get First Object On The List
// Do Not Use pApp->m_lpCmdLine Coz It Will Not Get Updated When OnFileOpen...
m_FilesListPosition = m_FilesList.GetHeadPosition();
m_FilesListPos = 1; // Coz We Are On The First Object On The List...
m_FilesListCount = m_FilesList.GetCount();

CString Clicked; // Make Sure We Count Spaces And UNICode...
Clicked.Format("%s", m_FilePathName);

// If File Was Clicked - Begin List Count From This File
if(Clicked.GetLength() > 0)
{
// Take Out Path Info...(Leave Only FileName)
Clicked.Format("%s", FileNameOnly(Clicked));

// Check If We Talking About a Wanted Extention
Extention.Format("%s", FileExtentionOnly(Clicked));
Extention.MakeUpper(); // Just In Case (Sensitive)...

// Check If It One Of These
if(IsWantedExtention(Extention))
{
for(int I=1; I < m_FilesListCount; I++) // Coz Starting From 1
{
Extention.Format("%s", m_FilesList.GetAt(m_FilesListPosition));

if(Clicked.CompareNoCase(Extention) == 0) break; // We Got It

m_FilesList.GetNext(m_FilesListPosition); // Move On The List
m_FilesListPos++;
}
}
}


/* DEBUG
CString S;

// Be Careful - Can READ List Objects If Not Exists
if(m_FilesListCount > 0)
{
S.Format("%d Picture Items On This Directory\t\nClicked = %d", m_FilesListCount, m_FilesListPos);
AfxMessageBox(S);
S.Format("First = \"%s\"", m_FilesList.GetAt(m_FilesListPosition));
AfxMessageBox(S);
}

if(m_FilesListCount > 1)
{
S.Format("Last = \"%s\"", m_FilesList.GetTail());
AfxMessageBox(S);
}
*/

return(TRUE);
}


Best Regards - Yovav Gad
EMail: Temp@Yovav.com
Web-Site: www.Yovav.com
QuestionWhy can't I compile the rc file? Pin
kes16-Apr-02 20:41
kes16-Apr-02 20:41 
AnswerRe: Why can't I compile the rc file? Pin
25-Apr-02 20:11
suss25-Apr-02 20:11 
GeneralRe: Why can't I compile the rc file? Pin
13-May-02 13:32
suss13-May-02 13:32 
AnswerRe: Why can't I compile the rc file? Pin
14-May-02 4:21
suss14-May-02 4:21 
GeneralDialog Pin
12-Apr-02 22:00
suss12-Apr-02 22:00 
GeneralRe: Dialog Pin
25-Apr-02 20:05
suss25-Apr-02 20:05 
GeneralRe: Dialog Pin
noudard2-Jul-02 11:22
noudard2-Jul-02 11:22 
GeneralRe: Dialog Pin
KienNT7815-Apr-08 16:12
KienNT7815-Apr-08 16:12 
Generalrotate Pin
30-Mar-02 9:32
suss30-Mar-02 9:32 
GeneralFrom where we can find the info abt: Pin
9-Mar-02 14:01
suss9-Mar-02 14:01 
Generaljpeg in a memory DC Pin
16-Feb-02 3:07
suss16-Feb-02 3:07 
GeneralDouble buffer implementation Pin
24-Jan-02 7:34
suss24-Jan-02 7:34 
GeneralRe: Double buffer implementation Pin
Yovav24-Jan-02 9:28
Yovav24-Jan-02 9:28 
GeneralConvrsion Pin
23-Jan-02 22:03
suss23-Jan-02 22:03 
GeneralRe: Convrsion Pin
Yovav24-Jan-02 1:48
Yovav24-Jan-02 1:48 
QuestionWhat poropse of this program? Pin
22-Jan-02 22:12
suss22-Jan-02 22:12 
GeneralPalette Pin
20-Jan-02 5:57
suss20-Jan-02 5:57 

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.