Click here to Skip to main content
6,635,160 members and growing! (15,892 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Bitmaps     Advanced

Streaming IPicture to Compound Document file

By Eugene Khodakovsky

Serialization of IPicture object
VC6Win2K, MFC, ATL, Dev
Posted:28 Oct 2001
Views:80,575
Bookmarked:37 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.54 Rating: 4.71 out of 5
1 vote, 11.1%
1

2

3
1 vote, 11.1%
4
7 votes, 77.8%
5

Sample Image - PictureStream.jpg

Introduction

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.

Classes

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

	virtual  void Copy( CPictureObj &right);
	virtual  HRESULT WriteToStream(IStream* pStream);
	virtual  HRESULT ReadFromStream(IStream* pStream);
	
// Attributes

public:
	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

public:

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

protected:

	void CalcZoom();
	void SetZoomToPosition();

protected:

	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 :
	~CPictureObjList();

	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;
pObj->CreateFromFile(CPoint(10,10));
m_objects.AddHead(pObj);

Also, you can load picture directly from the file:

CPictureObj* pObj =  new CPictureObj;
pObj->Load("MyFile.jpg");
m_objects.AddHead(pObj);

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

CPictureObj* pObj = m_objects.FindSelected();
if(pObj)
{
    pObj->ZoomIn();
}

Moving the selected picture is easy:

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

Drawing a collection of pictures is also easy:

GetDocument()->m_objects.Draw(pDC);

Implementation of picture serialization you can get from here:

void CYourDoc::Serialize(CArchive& ar)
{
	CWaitCursor wait;
	COleDocument::Serialize(ar);
	if (ar.IsStoring())
	{
		m_objects.WriteToStorage(m_lpRootStg); 
	}
	else
	{
		m_objects.ReadFromStorage(m_lpRootStg); 
		m_objects.DeselectAll();
	}
}

Good luck!

License

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


Member

Occupation: Software Developer (Senior)
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 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralPrint Preview save as jpg Pinmembernaraimha20:20 23 Apr '07  
QuestionStoring a resource bitmap using IPicture PinmemberD. L. Sleeman21:51 10 Jan '07  
GeneralRotating an Image using IPicture PinmemberAlex Evans17:32 3 Jan '05  
GeneralSave As a File such as bmp,jpg,ico... Pinmemberyuanque16:25 24 Jul '03  
GeneralHandle To Bitamp from IPicture PinmemberProjectX1:27 25 Apr '02  
GeneralRe: Handle To Bitamp from IPicture PinmemberEugeneK3:31 25 Apr '02  
GeneralRe: Handle To Bitamp from IPicture PinmemberProjectX22:12 28 Apr '02  
GeneralRe: Handle To Bitamp from IPicture PinmemberAlex Hazanov5:45 10 Dec '02  
GeneralRe: Handle To Bitamp from IPicture Pinmembernetecho1:25 17 Mar '05  
Generaljpeg in memory dc PinmemberAnonymous4:09 16 Feb '02  
GeneralRe: jpeg in memory dc Pinmemberswinefeaster18:06 11 May '02  

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

PermaLink | Privacy | Terms of Use
Last Updated: 28 Oct 2001
Editor: Chris Maunder
Copyright 2001 by Eugene Khodakovsky
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project