VC수업 Report로 나왔던 간단한 이미지 저장 루틴입니다. 참고하세요.
class CTestView : public CScrollView { protected: // create from serialization only CTestView(); DECLARE_DYNCREATE(CTestView) // Attributes public: CTestDoc* GetDocument(); // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual void OnInitialUpdate(); // called first time after construct virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); //}}AFX_VIRTUAL // Implementation public: virtual ~CTestView(); unsigned char m_ViewImg[250][250]; #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CTestView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: int m_penColor; int m_nWidth; BOOL m_bPressed; CPoint m_ptOld; }; void CTestView::OnDraw(CDC* pDC) { CTestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here for(int y=0;y<250;y++) { for(int x=0;x<250;x++){ pDC->SetPixel(x,y,RGB(m_ViewImg[y][x],m_ViewImg[y][x],m_ViewImg[y][x])); } } } void CTestView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal; // TODO: calculate the total size of this view CTestDoc* pDoc=GetDocument(); for(int x=0;x<250;x++){ for(int y=0;y<250;y++){ m_ViewImg[x][y]=pDoc->m_OpenImg[x][y]; pDoc->m_WriteImg[x][y] = m_ViewImg[x][y]; } } sizeTotal.cx = sizeTotal.cy = 100; SetScrollSizes(MM_TEXT, sizeTotal); } CTestView::CTestView() { // TODO: add construction code here m_ptOld=CPoint(0,0); m_bPressed=FALSE; m_nWidth=3; m_penColor=RGB(0,0,255); } class CTestDoc : public CDocument { protected: // create from serialization only CTestDoc(); DECLARE_DYNCREATE(CTestDoc) // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestDoc) public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); virtual BOOL OnSaveDocument(LPCTSTR lpszPathName); //}}AFX_VIRTUAL // Implementation public: virtual ~CTestDoc(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif unsigned char m_OpenImg[250][250]; unsigned char m_WriteImg[250][250]; protected: // Generated message map functions protected: //{{AFX_MSG(CTestDoc) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() private: }; void CTestDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here ar.Write(m_WriteImg,250*250); } else { // TODO: add loading code here CFile*fp=ar.GetFile(); if(fp->GetLength()!=250*250) { AfxMessageBox("250*250 이미지가 아닙니다"); return; } ar.Read(m_OpenImg,fp->GetLength()); } }
class CTestView : public CScrollView { protected: // create from serialization only CTestView(); DECLARE_DYNCREATE(CTestView) // Attributes public: CTestDoc* GetDocument(); // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual void OnInitialUpdate(); // called first time after construct virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); //}}AFX_VIRTUAL // Implementation public: virtual ~CTestView(); unsigned char m_ViewImg[250][250]; #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CTestView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: int m_penColor; int m_nWidth; BOOL m_bPressed; CPoint m_ptOld; }; void CTestView::OnDraw(CDC* pDC) { CTestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here for(int y=0;y<250;y++) { for(int x=0;x<250;x++){ pDC->SetPixel(x,y,RGB(m_ViewImg[y][x],m_ViewImg[y][x],m_ViewImg[y][x])); } } } void CTestView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal; // TODO: calculate the total size of this view CTestDoc* pDoc=GetDocument(); for(int x=0;x<250;x++){ for(int y=0;y<250;y++){ m_ViewImg[x][y]=pDoc->m_OpenImg[x][y]; pDoc->m_WriteImg[x][y] = m_ViewImg[x][y]; } } sizeTotal.cx = sizeTotal.cy = 100; SetScrollSizes(MM_TEXT, sizeTotal); } CTestView::CTestView() { // TODO: add construction code here m_ptOld=CPoint(0,0); m_bPressed=FALSE; m_nWidth=3; m_penColor=RGB(0,0,255); } class CTestDoc : public CDocument { protected: // create from serialization only CTestDoc(); DECLARE_DYNCREATE(CTestDoc) // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestDoc) public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); virtual BOOL OnSaveDocument(LPCTSTR lpszPathName); //}}AFX_VIRTUAL // Implementation public: virtual ~CTestDoc(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif unsigned char m_OpenImg[250][250]; unsigned char m_WriteImg[250][250]; protected: // Generated message map functions protected: //{{AFX_MSG(CTestDoc) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() private: }; void CTestDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here ar.Write(m_WriteImg,250*250); } else { // TODO: add loading code here CFile*fp=ar.GetFile(); if(fp->GetLength()!=250*250) { AfxMessageBox("250*250 이미지가 아닙니다"); return; } ar.Read(m_OpenImg,fp->GetLength()); } }