MFC中用OPENCV如何将图片显示在指定位置
voidCTestShowInMFCWindowDlg::OnBnClickedBtnOpen2(){charszFilter[]="Allfiles(*.*)|*.*|...
void CTestShowInMFCWindowDlg::OnBnClickedBtnOpen2()
{
char szFilter[]="All files(*.*)|*.*||";
CFileDialog ofd(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(ofd.DoModal()!=IDOK) return;
IplImage *src=cvLoadImage(ofd.GetPathName(),CV_LOAD_IMAGE_ANYCOLOR);
if(!src) return;
ShowMFC(src,GetDC());
}
这是我读取图片的按钮代码,我想知道应该怎么样才能让图片显示在指定的坐标,最好修改以上代码,谢谢 展开
{
char szFilter[]="All files(*.*)|*.*||";
CFileDialog ofd(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(ofd.DoModal()!=IDOK) return;
IplImage *src=cvLoadImage(ofd.GetPathName(),CV_LOAD_IMAGE_ANYCOLOR);
if(!src) return;
ShowMFC(src,GetDC());
}
这是我读取图片的按钮代码,我想知道应该怎么样才能让图片显示在指定的坐标,最好修改以上代码,谢谢 展开
2个回答
展开全部
最好是给待显示的图片指定一个父窗口,比如在对话框上加一个picture控件。
给你一段代码你参考一下:
void DisplayImage(CDC *pDC, CRect *pRc, LPCTSTR szImagePath)
{
pDC->FillSolidRect(pRc,RGB(255,255,255));
//从指定的路径szImagePath中读取文件句柄
HANDLE hFile
= CreateFile(szImagePath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
//获得图片文件的大小,用来分配全局内存
DWORD dwFileSize=GetFileSize(hFile, NULL);
//给图片分配全局内存
HGLOBAL hImageMemory=GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
//锁定内存
void *pImageMemory=GlobalLock(hImageMemory);
//保存实际读取的文件大小
DWORD dwReadedSize;
//读取图片到全局内存当中
ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL);
GlobalUnlock(hImageMemory); //解锁内存
CloseHandle(hFile); //关闭文件句柄
IStream *pIStream;//创建一个IStream接口指针,用来保存图片流
IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象
//用全局内存初使化IStream接口指针
CreateStreamOnHGlobal(hImageMemory, false, &pIStream);
//用OleLoadPicture获得IPicture接口指针
HRESULT hrs
= OleLoadPicture( pIStream, 0, false, IID_IPicture, (LPVOID*)&pIPicture );
if ( SUCCEEDED( hrs ) )
{
//得到IPicture COM接口对象后,你就可以进行获得图片信息、显示图片等操作
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高
pIPicture->get_Height(&hmHeight);
//在指定的DC上绘出图片(按目标窗口大小拉伸)
const long x0 = pRc->left;
const long y0 = pRc->top;
const long w = pRc->Width();
const long h = pRc->Height();
/*
pIPicture->Render(pDC->m_hDC,x0,y0,w,h,0,hmHeight,hmWidth,-hmHeight,NULL);
*/
//按图片原始大小显示
double fX,fY;
fX = (double)pDC->GetDeviceCaps(HORZRES) * (double)hmWidth /
((double)pDC->GetDeviceCaps(HORZSIZE) * 100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES) * (double)hmHeight /
((double)pDC->GetDeviceCaps(VERTSIZE) * 100.0);
if (fX > w || fY > h)
{
double dPercent = 1.0;
double dPercentx = w / fX;
double dPercenty = h / fY;
dPercent = (dPercentx > dPercenty) ? dPercenty : dPercentx;
fX *= dPercent;
fY *= dPercent;
}
int nX = int ( ((double)w - fX) / 2.0 );
int nY = int ( ((double)h - fY) / 2.0 );
//use render function display image
pIPicture->Render(pDC->m_hDC,
nX,nY,(DWORD)fX,(DWORD)fY,
0,hmHeight,hmWidth,-hmHeight,NULL);
GlobalFree(hImageMemory); //释放全局内存
pIStream->Release(); //释放pIStream
pIPicture->Release(); //释放pIPicture
}
}
else
{
int nCenterx = (pRc->Width() - pRc->left) / 2;
int nCentery = (pRc->Height() - pRc->top) / 2;
pDC->SetTextAlign(TA_CENTER);
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(nCenterx, nCentery, _T("图像文件已丢失"));
}
}
给你一段代码你参考一下:
void DisplayImage(CDC *pDC, CRect *pRc, LPCTSTR szImagePath)
{
pDC->FillSolidRect(pRc,RGB(255,255,255));
//从指定的路径szImagePath中读取文件句柄
HANDLE hFile
= CreateFile(szImagePath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
//获得图片文件的大小,用来分配全局内存
DWORD dwFileSize=GetFileSize(hFile, NULL);
//给图片分配全局内存
HGLOBAL hImageMemory=GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
//锁定内存
void *pImageMemory=GlobalLock(hImageMemory);
//保存实际读取的文件大小
DWORD dwReadedSize;
//读取图片到全局内存当中
ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL);
GlobalUnlock(hImageMemory); //解锁内存
CloseHandle(hFile); //关闭文件句柄
IStream *pIStream;//创建一个IStream接口指针,用来保存图片流
IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象
//用全局内存初使化IStream接口指针
CreateStreamOnHGlobal(hImageMemory, false, &pIStream);
//用OleLoadPicture获得IPicture接口指针
HRESULT hrs
= OleLoadPicture( pIStream, 0, false, IID_IPicture, (LPVOID*)&pIPicture );
if ( SUCCEEDED( hrs ) )
{
//得到IPicture COM接口对象后,你就可以进行获得图片信息、显示图片等操作
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高
pIPicture->get_Height(&hmHeight);
//在指定的DC上绘出图片(按目标窗口大小拉伸)
const long x0 = pRc->left;
const long y0 = pRc->top;
const long w = pRc->Width();
const long h = pRc->Height();
/*
pIPicture->Render(pDC->m_hDC,x0,y0,w,h,0,hmHeight,hmWidth,-hmHeight,NULL);
*/
//按图片原始大小显示
double fX,fY;
fX = (double)pDC->GetDeviceCaps(HORZRES) * (double)hmWidth /
((double)pDC->GetDeviceCaps(HORZSIZE) * 100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES) * (double)hmHeight /
((double)pDC->GetDeviceCaps(VERTSIZE) * 100.0);
if (fX > w || fY > h)
{
double dPercent = 1.0;
double dPercentx = w / fX;
double dPercenty = h / fY;
dPercent = (dPercentx > dPercenty) ? dPercenty : dPercentx;
fX *= dPercent;
fY *= dPercent;
}
int nX = int ( ((double)w - fX) / 2.0 );
int nY = int ( ((double)h - fY) / 2.0 );
//use render function display image
pIPicture->Render(pDC->m_hDC,
nX,nY,(DWORD)fX,(DWORD)fY,
0,hmHeight,hmWidth,-hmHeight,NULL);
GlobalFree(hImageMemory); //释放全局内存
pIStream->Release(); //释放pIStream
pIPicture->Release(); //释放pIPicture
}
}
else
{
int nCenterx = (pRc->Width() - pRc->left) / 2;
int nCentery = (pRc->Height() - pRc->top) / 2;
pDC->SetTextAlign(TA_CENTER);
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(nCenterx, nCentery, _T("图像文件已丢失"));
}
}
展开全部
void CTestShowInMFCWindowDlg::OnBnClickedBtnOpen2()
{
char szFilter[]="All files(*.*)|*.*||";
CDC *pDC = GetDC();
CRect rect;
CFileDialog ofd(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(ofd.DoModal()!=IDOK) return;
IplImage *src=cvLoadImage(ofd.GetPathName(),CV_LOAD_IMAGE_ANYCOLOR);
if(!src) return;
GetClientRect(&rect);
BITMAPINFO bmi;
FillBitmapInfo(&bmi, src->width,src->height, 32);
::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,src->width,src->height,0,0,
src->width,src->height,src->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
ShowMFC(src,GetDC());
}
void CTestShowInMFCWindowDlg::FillBitmapInfo( BITMAPINFO *bmi, int width, int height, int bpp )
{
ASSERT( bmi && width > 0 && height > 0 &&
(bpp == 8 || bpp == 24 || bpp == 32) );
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
memset( bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8 )
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for( i = 0; i < 256; i++ )
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
大概是这个意思吧
{
char szFilter[]="All files(*.*)|*.*||";
CDC *pDC = GetDC();
CRect rect;
CFileDialog ofd(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(ofd.DoModal()!=IDOK) return;
IplImage *src=cvLoadImage(ofd.GetPathName(),CV_LOAD_IMAGE_ANYCOLOR);
if(!src) return;
GetClientRect(&rect);
BITMAPINFO bmi;
FillBitmapInfo(&bmi, src->width,src->height, 32);
::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,src->width,src->height,0,0,
src->width,src->height,src->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
ShowMFC(src,GetDC());
}
void CTestShowInMFCWindowDlg::FillBitmapInfo( BITMAPINFO *bmi, int width, int height, int bpp )
{
ASSERT( bmi && width > 0 && height > 0 &&
(bpp == 8 || bpp == 24 || bpp == 32) );
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
memset( bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8 )
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for( i = 0; i < 256; i++ )
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
大概是这个意思吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询