Click here to Skip to main content
13,295,429 members (51,047 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 28 Oct 2001

Streaming IPicture to Compound Document file

, 28 Oct 2001
Rate this:
Please Sign up or sign in to vote.
Serialization of IPicture object
<!-- Download Links --> <!-- Article image -->

Sample Image - PictureStream.jpg

<!-- Add the rest of your HTML here -->


I really like the IPicture object. This COM object works very well. However, I met some problems when I was trying to use this object in the Document-View architecture. The main problem is implementation of serialization. I built two classes to resolve those problems. The first class called CPictureObj. This class is responsible for drawing picture. The second one is collection of picture objects. By using both of them it is easy to implement the following functionality:

  • Load one or more images into document (*.bmp, *.jpeg, *.ico,...)
  • Scaling images (Zoom In, Zoom Out)
  • Move images by cursor
  • Alignment
  • Save/Load images to/from compound document file.


class <code>CPictureObj  :	public CPictureHolder, public CObject
	CPictureObj(const CRect position);
	virtual  ~CPictureObj();
	void RemoveAndDestroy();

	virtual  void Copy( CPictureObj &right);
	virtual  HRESULT WriteToStream(IStream* pStream);
	virtual  HRESULT ReadFromStream(IStream* pStream);
// Attributes
	HorzAlign	GetHorzAlign( void ) const;
	void		SetHorzAlign( HorzAlign eHorzAlign );
	VertAlign	GetVertAlign( void ) const;
	void		SetVertAlign( VertAlign eVertAlign );
	BOOL		GetSelected( void ) const;
	void		SetSelected( BOOL bValue );
	void		SetVisible(BOOL bValue);
	BOOL		GetVisible();
	CRect		GetPosition();
	void		SetPosition(const CRect pos);
	CRect		GetStartRect();
	void		SetStartRect(const CRect pos);
	CString		GetPathName();
	void		SetPathName(const CString pathname);

	OLE_HANDLE	GetHandle();
	CSize		GetSize();			// in himetric
	CSize		GetSize(CDC*pDC);	// in pixel
	CSize		GetSize(CWnd*pWnd);	// in pixel
	BOOL		IsValidPicture();

// Operations

	BOOL Load(LPCTSTR szFile);
	BOOL CreateFromFile(const CPoint pt);
	void ReleasePicture();
	void MoveTo(const CRect& position, CWnd* pView);

	// Drawing picture
	void Draw(CDC* pDC);
	void Draw(CDC* pDC, CRect& rcBounds);
	void Draw(CDC* pDC, const CRect& rcPosition, const CRect& rcBounds);
	void DrawTracker(CDC* pDC);
	void DrawTracker(CDC* pDC, const CRect& rect);
	void ZoomIn();
	void ZoomOut();


	void CalcZoom();
	void SetZoomToPosition();


	HorzAlign	m_eHorizAlign;
	VertAlign	m_eVertAlign;
	BOOL		m_bSelected;
	BOOL		m_bVisible;
	CRect		m_rcPosition;		// in pixels
	CRect		m_startRect;		// in pixels
	int		m_trackerStyle;
	int		m_zoomX,m_zoomY;

	// Not serialized
	CString		m_strPathName;


class  CPictureObjList : public CTypedPtrList {CObList, CPictureObj*}
public :

	void RemoveAndDestroy();
	void DeselectAll();
	CSize ComputeMaxSize();			// in himetric
	CSize ComputeMaxSize(CDC* pDC);		// in pixel
	CSize ComputeMaxSize(CWnd* pWnd);	// in pixel
	CRect GetRect();			// in pixel

	CPictureObj* FindSelected();
	CPictureObj* ObjectAt(const CPoint& pt);
	bool Remove(CPictureObj* pObj);
	void Draw(CDC* pDC);

	// Streaming
	HRESULT WriteToStream(IStream* pStream);
	HRESULT ReadFromStream(IStream* pStream);
	BOOL	WriteToStorage(LPSTORAGE lpRootStg);
	BOOL	ReadFromStorage(LPSTORAGE lpRootStg);


Using the classes

For use streaming picture create your document object derived from COleDocument.

class CYourDoc : public COleDocument

Inside of the document object define a picture collection like here:

CPictureObjList	m_objects;

Now, you can insert picture by this simple method:

CPictureObj* pObj =  new CPictureObj;

Also, you can load picture directly from the file:

CPictureObj* pObj =  new CPictureObj;

For scaling pictures you can use the ZoomIn, ZoomOut member functions:

CPictureObj* pObj = m_objects.FindSelected();

Moving the selected picture is easy:

CPoint delta = point - c_last;
CPictureObj* pSel = m_objects.FindSelected();
 CRect position = pSel->GetPosition();
 position += delta;
 pSel->MoveTo(position, this);

Drawing a collection of pictures is also easy:


Implementation of picture serialization you can get from here:

void CYourDoc::Serialize(CArchive& ar)
	CWaitCursor wait;
	if (ar.IsStoring())

Good luck!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Eugene Khodakovsky
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPrint Preview save as jpg Pin
naraimha23-Apr-07 20:20
membernaraimha23-Apr-07 20:20 
QuestionStoring a resource bitmap using IPicture Pin
D. L. Sleeman10-Jan-07 21:51
memberD. L. Sleeman10-Jan-07 21:51 
GeneralRotating an Image using IPicture Pin
Alex Evans3-Jan-05 17:32
memberAlex Evans3-Jan-05 17:32 
GeneralSave As a File such as bmp,jpg,ico... Pin
yuanque24-Jul-03 16:25
memberyuanque24-Jul-03 16:25 
GeneralHandle To Bitamp from IPicture Pin
ProjectX25-Apr-02 1:27
memberProjectX25-Apr-02 1:27 
GeneralRe: Handle To Bitamp from IPicture Pin
EugeneK25-Apr-02 3:31
memberEugeneK25-Apr-02 3:31 
GeneralRe: Handle To Bitamp from IPicture Pin
ProjectX28-Apr-02 22:12
memberProjectX28-Apr-02 22:12 
GeneralRe: Handle To Bitamp from IPicture Pin
Alex Hazanov10-Dec-02 5:45
memberAlex Hazanov10-Dec-02 5:45 
GeneralRe: Handle To Bitamp from IPicture Pin
netecho17-Mar-05 1:25
membernetecho17-Mar-05 1:25 
Generaljpeg in memory dc Pin
Anonymous16-Feb-02 4:09
memberAnonymous16-Feb-02 4:09 
GeneralRe: jpeg in memory dc Pin
swinefeaster11-May-02 18:06
memberswinefeaster11-May-02 18:06 
I don't think you can do this directly. You need to load in a jpg, and then decompress it into a bitmap. Then you can select it into a dc. If you want it back as a jpg, you need to recompress it as a jpg and save it to file.



Check out Aephid Photokeeper, the powerful digital
photo album solution at <a href = http:\\\photokeeper-default-frame.html>

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 29 Oct 2001
Article Copyright 2001 by Eugene Khodakovsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid