Click here to Skip to main content
6,932,036 members and growing! (19,641 online)
Email Password   helpLost your password?
 
Multimedia » General Graphics » Image Display     Intermediate License: The Code Project Open License (CPOL)

CPicture - The Yovav (Horror) PictureShow

By Yovav

Routines for displaying image files (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
VC6Win2K, MFC, Dev
Revision:2 (See All)
Posted:3 Oct 2001
Updated:18 Jun 2003
Views:255,333
Bookmarked:83 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
69 votes for this article.
Popularity: 8.37 Rating: 4.55 out of 5

1
1 vote, 3.6%
2
3 votes, 10.7%
3

4
24 votes, 85.7%
5

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)

About the Author

Yovav


Member
CEO and founder of MicroMighty, Inc.

MicroMighty, Inc. - Software products, ASP.NET Components for developers, Social network services and more!
Occupation: CEO
Company: MicroMighty, Inc.
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 118 (Total in Forum: 118) (Refresh)FirstPrevNext
Generalimage in case of dialog box apps PinmemberSadru21:29 1 Mar '09  
The code for dialog box:

void cLiveJPEG::OnPaint()
{

CPaintDC dc(this); // device context for painting
CRect rect;
GetDlgItem(IDC_STATIC_MJPEGWIN)->GetClientRect(&rect);
CWnd *pWnd = (CWnd*)GetDlgItem(IDC_STATIC_MJPEGWIN);
if (pWnd){
CDC *pDC = pWnd->GetDC();
CMemDC cDC(pDC);

m_cPict->Show(&cDC, rect);
}

}


#ifndef _MEMDC_H_
#define _MEMDC_H_

//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email: keithr@europa.com
// Copyright 1996-1999, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
// Added print support. - KR
//
// 11/3/99 Fixed most common complaint. Added
// background color fill. - KR
//
// 11/3/99 Added support for mapping modes other than
// MM_TEXT as suggested by Lee Sang Hun. - KR
//
// This class implements a memory Device Context which allows
// flicker free drawing.

class CMemDC : public CDC {
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:

CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
{
ASSERT(pDC != NULL);

// Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();

// Get the rectangle to draw
if (pRect == NULL) {
pDC->GetClipBox(&m_rect);
} else {
m_rect = *pRect;
}

if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);

m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);

SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
} else {
// Make a copy of the relevent parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}

// Fill background
FillSolidRect(m_rect, pDC->GetBkColor());
}


~CMemDC()
{
if (m_bMemDC) {
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);

//Swap back the original bitmap.
SelectObject(m_oldBitmap);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}

// Allow usage as a pointer
CMemDC* operator->()
{
return this;
}

// Allow usage as a pointer
operator CMemDC*()
{
return this;
}
};

#endif
GeneralHow to rotate ? Pinmemberhujunxi17:04 8 May '08  
GeneralJust one word PinmemberDemian Panello16:17 7 Jun '07  
GeneralThanks PinmemberKaworu223:34 21 Mar '06  
GeneralHow do i use it for IDC_STATIC control? Pinmemberetboite23:05 10 Nov '05  
GeneralRe: How do i use it for IDC_STATIC control? PinmemberK(arl)21:56 23 Feb '06  
GeneralRe: How do i use it for IDC_STATIC control? Pinmemberserup4:52 18 Sep '08  
GeneralDisplaying big images (need ScrollBar) Pinmemberangel42014:27 18 Aug '05  
GeneralPixel value ? Pinmemberangel42014:42 17 Aug '05  
GeneralRe: Pixel value ? PinmemberChristian Graus15:39 17 Aug '05  
GeneralRe: Pixel value ? Pinmemberangel42014:01 18 Aug '05  
GeneralNice Class PinmemberThatsAlok20:05 8 Jun '05  
GeneralCPicture to HBITMAP Pinmembernaragana16:36 23 May '05  
GeneralRe: CPicture to HBITMAP PinmemberThatsAlok1:46 20 Jun '05  
Generalbug in CPictureShowDoc::CreateFilesList Pinsussshay cohen23:22 4 Apr '05  
GeneralHelp!!!!! Pinmembersubbuviv0:24 31 Mar '05  
Generalchange pictures in Dialog Pinmemberolis5:31 7 Feb '05  
GeneralRotating an Image PinmemberAlex Evans17:42 3 Jan '05  
GeneralRe: Rotating an Image PinmemberEinsteinJ0:53 26 Sep '05  
GeneralLoading a picture from an ftp site Pinmemberrgarf4:48 3 Sep '04  
Generalproblem in image compression in database PinmemberKRANTHI KUMAR KUKKALA4:45 31 Aug '04  
Generalhow to get image data as pixels? Pinmembersgorgulu4:15 23 Aug '04  
Generalhelloo sir one problem PinmemberThatsAlok1:52 17 Jul '04  
GeneralIPictute and HBitmap PinmemberMirikos11:25 22 Jun '04  
GeneralRe: IPictute and HBitmap Pinmembermcanti23:05 29 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2003
Editor: Sean Ewington
Copyright 2001 by Yovav
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project