CBitmap 的两个函数 LoadBitmap(从文件装载图片) and GetBitmap(用位图信息填充BITMAP结构)
SetTimer(1,1000,NULL) 用 WM_TIMER消息 在Ontimer中响应 可以添加多个定时器 取消定时器 KillTimer(标志)
m_nTimer = SetTimer(1, 1000, NULL);
KillTimer(m_nTimer);
获得用户区域的矩形大小 GetClientRect获得客户矩形
CRect rectClient;
GetClientRect(&rectClient);
m_h = rectClient.Height();
m_w = rectClient.Width()/8;
在指定位置画图
CClientDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap* pOldBitmap = dcMem.SelectObject(&m_Bitmap[digit]);
dc.StretchBlt(m_w*pos, 0, m_w, m_h,
&dcMem, 0, 0, m_bm.bmWidth, m_bm.bmHeight, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
dcMem.DeleteDC( );
结果
Follow This is a method that troubles
void CPlay::OnBackground(CBitmap* p, CDC* pDC)
{
CDC dcMemory;
BITMAP bm;
dcMemory.CreateCompatibleDC(pDC);
dcMemory.SelectObject(p);
p->GetBitmap(&bm);
pDC->StretchBlt(0, 0, screenX, screenY, &dcMemory, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
}
variable.LoadBitmap(RESOURCE);
screenX = GetSystemMetrics(SM_CXSCREEN);
screenY = GetSystemMetrics(SM_CYSCREEN);
may be this one is good 给picture控件关联一个CStatic变量 就可以了
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
BITMAP bit;
bitmap.GetBitmap(&bit);
m_pic.SetBitmap(bitmap);
CRect *rect = new CRect();
m_pic.GetClientRect(rect);
CDC *pDC = m_pic.GetDC();
CDC m_DC;
m_DC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap = m_DC.SelectObject(&bitmap);
pDC->StretchBlt(0,0,rect->Width(),rect->Height(),&m_DC,0,0,bit.bmWidth,bit.bmHeight,SRCCOPY);
m_DC.SelectObject(pOldBitmap);
ReleaseDC(pDC);
And the result
If I comment the CDialog::OnPaint();
then we got
MFC OpenCv 显示图片
记录邹宇华大神的足迹
包括打开图片的写法
图片缩放
图片在Picture控件上面显示
显示图片主要代码 SetRect GetSafeHdc
void CMy222222Dlg::ShowImage( IplImage *img, UINT ID )
{
CDC* pDC = GetDlgItem( ID ) ->GetDC(); // 获得显示控件的 DC
HDC hDC = pDC ->GetSafeHdc(); // 获取 HDC(设备句柄) 来进行绘图操作
CRect rect;
GetDlgItem(ID) ->GetClientRect( &rect );
int rw = rect.right - rect.left; // 求出图片控件的宽和高
int rh = rect.bottom - rect.top;
int iw = img->width; // 读取图片的宽和高
int ih = img->height;
int tx = (int)(rw - iw)/2; // 使图片的显示位置正好在控件的正中
int ty = (int)(rh - ih)/2;
SetRect( rect, tx, ty, tx+iw, ty+ih );
CvvImage cimg;
cimg.CopyOf( img ); // 复制图片
cimg.DrawToHDC( hDC, &rect ); // 将图片绘制到显示控件的指定区域内
ReleaseDC( pDC );
}
按钮打开图片主要图片
// TODO: Add your control notification handler code here
CFileDialog dlg(
TRUE, _T("*.bmp"), NULL,
OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
_T("image files (*.bmp; *.jpg) |*.bmp; *.jpg | All Files (*.*) |*.*||"), NULL
); // 选项图片的约定
dlg.m_ofn.lpstrTitle = _T("Open Image"); // 打开文件对话框的标题名
if( dlg.DoModal() != IDOK ) // 判断是否获得图片
return;
CString mPath = dlg.GetPathName(); // 获取图片路径
IplImage* ipl = cvLoadImage( mPath, 1 ); // 读取图片、缓存到一个局部变量 ipl 中
if( !ipl ) // 判断是否成功载入图片
return;
if( TheImage ) // 对上一幅显示的图片数据清零
cvZero( TheImage );
ResizeImage( ipl ); // 对读入的图片进行缩放,使其宽或高最大值者刚好等于 256,再复制到 TheImage 中
ShowImage( TheImage, IDC_SHOWEPIC ); // 调用显示图片函数
cvReleaseImage( &ipl ); // 释放 ipl 占用的内存
缩放图像
void CMy222222Dlg::ResizeImage( IplImage *img )
{
// 读取图片的宽和高
int w = img->width;
int h = img->height;
// 找出宽和高中的较大值者
int max = (w > h)? w: h;
// 计算将图片缩放到TheImage区域所需的比例因子
float scale = (float) ( (float) max / 256.0f );
// 缩放后图片的宽和高
int nw = (int)( w/scale );
int nh = (int)( h/scale );
// 为了将缩放后的图片存入 TheImage 的正中部位,需计算图片在 TheImage 左上角的期望坐标值
int tlx = (nw > nh)? 0: (int)(256-nw)/2;
int tly = (nw > nh)? (int)(256-nh)/2: 0;
// 设置 TheImage 的 ROI 区域,用来存入图片 img
cvSetImageROI( TheImage, cvRect( tlx, tly, nw, nh) );
// 对图片 img 进行缩放,并存入到 TheImage 中
cvResize( img, TheImage );
// 重置 TheImage 的 ROI 准备读入下一幅图片
cvResetImageROI( TheImage );
}