Now I'm trying to display an image in view in the dialog in MFC.
The code as following.
void CTestview::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
CRect rcWin;
GetWindowRect( &rcWin );
double ff;
ff=rcWin.Width();
rcWin.Height();
DoDisplayImage();
}
and
void CTestview::DoDisplayImage(void)
{
CPoint pt;
CRect rectClient;
CDC * pDC;
pDC = GetDC();
GetClientRect(rectClient);
int ii;
ii=slider_val;
(void) MagickCore::SetClientPath(fileposition);
InitializeMagick(fileposition);
Image m_Image;
Image m_blurgray;
int _imageHeight;
try {
//C:\work\mfc_test5\mfc_test5
m_Image.read(fileposition);
}
catch(Exception)
{
return ;
}
char str_x[10];
char str_y[10];
if (pDC != NULL)
{
m_blurgray.gaussianBlur(20,2); //blur
int nImageY;
int nImageX;
CSize sizeScaled;
// Clear the background
pDC->FillSolidRect(rectClient,pDC->GetBkColor());
pt = rectClient.TopLeft();
nImageX= m_Image.columns() ;
//nImageY = m_Image.rows();
CPoint aa;
aa = rectClient.Size();
// Extract the pixels from Magick++ image object
PixelPacket *pPixels = m_Image.getPixels(0,0,m_Image.columns(),m_Image.rows());
// Set up the Windows bitmap header
BITMAPINFOHEADER bmi;
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biWidth =m_Image.columns();
bmi.biHeight = (-1)*m_Image.rows();
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = 0;
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
bmi.biClrUsed = 0;
bmi.biClrImportant = 0;
// Blast it to the device context
SetStretchBltMode(pDC->m_hDC,COLORONCOLOR);
StretchDIBits(pDC->m_hDC,
0,
0,
m_Image.columns(),
m_Image.rows(),
0,
0,
m_Image.columns(),
m_Image.rows(),
pPixels,
(BITMAPINFO *)&bmi,
DIB_RGB_COLORS,
SRCCOPY);
//UpdateData(FALSE);
}
}
The problem is that display image have so much delay when I display an image into view. I don't know exactly what should I do for solving this problem.
update 1
I have update the code as following.
#pragma once
#include <Magick++.h>
// CTestview view
class CTestview : public CScrollView
{
DECLARE_DYNCREATE(CTestview)
protected:
CTestview(); // protected constructor used by dynamic creation
virtual ~CTestview();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual void OnInitialUpdate(); // first time after construct
DECLARE_MESSAGE_MAP()
public:
void DoDisplayImage(void);
Image m_image
};
When I compile above code then I've got some error as following.
------ Build started: Project: mfc_test5, Configuration: Release Win32 ------
Build started
PrepareForBuild:
Creating directory "C:\work\mfc_test5\mfc_test5\Release\".
InitializeBuildStatus:
Creating "Release\mfc_test5.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
stdafx.cpp
mfc_test5.cpp
c:\program files\imagemagick-6.8.6-q8\include\magick/pixel-accessor.h(160): warning C4244: '=' : conversion from 'double' to 'MagickCore::MagickRealType', possible loss of data
c:\work\mfc_test5\mfc_test5\mfc_test5\Testview.h(33): error C2143: syntax error : missing ';' before '}'
mfc_test5Dlg.cpp
c:\program files\imagemagick-6.8.6-q8\include\magick/pixel-accessor.h(160): warning C4244: '=' : conversion from 'double' to 'MagickCore::MagickRealType', possible loss of data
c:\work\mfc_test5\mfc_test5\mfc_test5\Testview.h(33): error C2143: syntax error : missing ';' before '}'
C:\Program Files\Intel\plsuite\include\ipl.h(778): warning C4819: The file contains a character that cannot be represented in the current code page (949). Save the file in Unicode format to prevent data loss
C:\Program Files\Intel\plsuite\examples\Tutorial.IPL\IPLROOMS\COOKROOM\macros.inc(21): warning C4005: '_ASSERTE' : macro redefinition
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\crtdbg.h(213) : see previous definition of '_ASSERTE'
Build FAILED.
Time Elapsed 00:00:37.30
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have found some reference code as following.as you can see, the following code use Image m_image.
How could it use like that?
// NtMagickView.h : interface of the CNtMagickView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_NTMAGICKVIEW_H__8A45000C_6176_11D4_AC4F_400070168026__INCLUDED_)
#define AFX_NTMAGICKVIEW_H__8A45000C_6176_11D4_AC4F_400070168026__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define NTMAGICK_DEFEXT "*.JPG;*.JPEG"
#define NTMAGICK_ALL "All Files (*.*)|*.*|"
#define NTMAGICK_BMP "Bitmaps (*.BMP;*.RLE)|*.BMP;*.RLE|"
#define NTMAGICK_GIF "GIF (*.GIF)|*.GIF|"
#define NTMAGICK_TIF "TIF (*.TIF;*.TIFF)|*.TIF;*.TIFF|"
#define NTMAGICK_JPEG "JPEG (*.JPG;*.JPEG)|*.JPG;*.JPEG|"
#define NTMAGICK_ICON "Icons (*.ICO)|*.ICO|"
class CNtMagickView : public CView
{
protected: // create from serialization only
CNtMagickView();
DECLARE_DYNCREATE(CNtMagickView)
// Attributes
public:
CString m_szFile;
Image * m_pImage;
CNtMagickDoc* GetDocument();
void DoDisplayError(CString szFunction, CString szCause);
void DoDisplayImage();
BOOL DoReadImage();
CSize Scale(CSize sizeSrc, CSize sizeTgt);
float ScaleFactor(BOOL bAllowZoom, CSize sizeSrc, CSize sizeTgt);
void UpdateUI(CCmdUI* pCmdUI);
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CNtMagickView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CNtMagickView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CNtMagickView)
afx_msg void OnFileOpen();
afx_msg void OnImageFlipHorizontal();
afx_msg void OnUpdateImageFlipHorizontal(CCmdUI* pCmdUI);
afx_msg void OnImageFlipVertical();
afx_msg void OnUpdateImageFlipVertical(CCmdUI* pCmdUI);
afx_msg void OnImageRotate180();
afx_msg void OnUpdateImageRotate180(CCmdUI* pCmdUI);
afx_msg void OnImageRotate90();
afx_msg void OnUpdateImageRotate90(CCmdUI* pCmdUI);
afx_msg void OnImageRotate90ccw();
afx_msg void OnUpdateImageRotate90ccw(CCmdUI* pCmdUI);
afx_msg void OnFileClear();
afx_msg void OnUpdateFileClear(CCmdUI* pCmdUI);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in NtMagickView.cpp
inline CNtMagickDoc* CNtMagickView::GetDocument()
{ return (CNtMagickDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_NTMAGICKVIEW_H__8A45000C_6176_11D4_AC4F_400070168026__INCLUDED_)
Update 2
I have edited like as following but it is not called ever. is this right? when do it call this function?
void CTestview::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
m_pimage.read(fileposition);
}