VC中图片显示
我想通过点击按钮,在制定的静态文本框中将预先设定的图片显示出来,我写的代码如下:CStatic*cWm=(CStatic*)GetDlgItem(IDC_PICB);CB...
我想通过点击按钮,在制定的静态文本框中将预先设定的图片显示出来,我写的代码如下:
CStatic *cWm = (CStatic *)GetDlgItem(IDC_PICB);
CBitmap bmpload;
bmpload.LoadBitmapW(IDB_BITMAP1);
cWm->ModifyStyle(0,SS_BITMAP);
cWm->SetBitmap(HBITMAP(bmpload.Detach()));
其中IDC_PICB是指定的静态文本框的ID,IDB_BITMAP1是导入的bmp格式的图片。这样编译能通过,但是点击按钮时,却没有显示图片,希望高手指点指点。谢谢! 展开
CStatic *cWm = (CStatic *)GetDlgItem(IDC_PICB);
CBitmap bmpload;
bmpload.LoadBitmapW(IDB_BITMAP1);
cWm->ModifyStyle(0,SS_BITMAP);
cWm->SetBitmap(HBITMAP(bmpload.Detach()));
其中IDC_PICB是指定的静态文本框的ID,IDB_BITMAP1是导入的bmp格式的图片。这样编译能通过,但是点击按钮时,却没有显示图片,希望高手指点指点。谢谢! 展开
展开全部
CString str_Photo = theApp.m_UserPhoto ;
if ((str_Photo.Right(3)==_T("bmp"))||(str_Photo.Right(3)==_T("BMP")))
{
CDC *pDC = m_Picture.GetDC();
CRect m_Rect;
m_Picture.GetClientRect(&m_Rect);
CDC dc;
dc.CreateCompatibleDC(pDC);//CreateCompatibleDC
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(NULL,str_Photo,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
CBitmap m_Bitmap;
m_Bitmap.Attach(hBitmap);//loadBitmap;
dc.SelectObject(&m_Bitmap);
BITMAP bmp;
m_Bitmap.GetBitmap(&bmp);
pDC->StretchBlt(m_Rect.left,m_Rect.top,m_Rect.Width() ,m_Rect.Height(),&dc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
dc.DeleteDC();
}
else if ((str_Photo.Right(3)==_T("jpg"))||(str_Photo.Right(3)==_T("JPG")))
{
IStream *m_pStream;//定义流对象
IPicture *m_pPicture;//定义接口对象
OLE_XSIZE_HIMETRIC m_JpgWidth; //jpg width
OLE_XSIZE_HIMETRIC m_JpgHeight;//jpg height
HGLOBAL hMem; //定义堆句柄
CFile m_File;
m_File.Open(str_Photo,CFile::modeReadWrite); ///读文件
ULONGLONG filesize = m_File.GetLength();///get file length
hMem = GlobalAlloc(GMEM_MOVEABLE,filesize);///alloc meomery
LPVOID pData = NULL;
pData = GlobalLock(hMem); //lock the meomry arey
m_File.Read(pData,filesize);///read file to the meomery
m_File.Close();
GlobalUnlock(hMem);
CreateStreamOnHGlobal(hMem,TRUE,&m_pStream);
OleLoadPicture(m_pStream,filesize,TRUE,IID_IPicture,(LPVOID*)&m_pPicture);
m_pPicture->get_Width(&m_JpgWidth);
m_pPicture->get_Height(&m_JpgHeight);
CDC *pDC = m_Picture.GetDC();
CDC dc;
dc.CreateCompatibleDC(pDC);
CRect rect;
m_Picture.GetClientRect(&rect);
m_pPicture->Render(pDC->m_hDC,rect.left,rect.top,rect.Width(),rect.Height(),0,m_JpgHeight,m_JpgWidth,-m_JpgHeight,NULL);
GlobalFree(hMem);
DeleteDC(dc);
}
这是读取图片路径读取BMP或JPG图像。并显示
if ((str_Photo.Right(3)==_T("bmp"))||(str_Photo.Right(3)==_T("BMP")))
{
CDC *pDC = m_Picture.GetDC();
CRect m_Rect;
m_Picture.GetClientRect(&m_Rect);
CDC dc;
dc.CreateCompatibleDC(pDC);//CreateCompatibleDC
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(NULL,str_Photo,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
CBitmap m_Bitmap;
m_Bitmap.Attach(hBitmap);//loadBitmap;
dc.SelectObject(&m_Bitmap);
BITMAP bmp;
m_Bitmap.GetBitmap(&bmp);
pDC->StretchBlt(m_Rect.left,m_Rect.top,m_Rect.Width() ,m_Rect.Height(),&dc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
dc.DeleteDC();
}
else if ((str_Photo.Right(3)==_T("jpg"))||(str_Photo.Right(3)==_T("JPG")))
{
IStream *m_pStream;//定义流对象
IPicture *m_pPicture;//定义接口对象
OLE_XSIZE_HIMETRIC m_JpgWidth; //jpg width
OLE_XSIZE_HIMETRIC m_JpgHeight;//jpg height
HGLOBAL hMem; //定义堆句柄
CFile m_File;
m_File.Open(str_Photo,CFile::modeReadWrite); ///读文件
ULONGLONG filesize = m_File.GetLength();///get file length
hMem = GlobalAlloc(GMEM_MOVEABLE,filesize);///alloc meomery
LPVOID pData = NULL;
pData = GlobalLock(hMem); //lock the meomry arey
m_File.Read(pData,filesize);///read file to the meomery
m_File.Close();
GlobalUnlock(hMem);
CreateStreamOnHGlobal(hMem,TRUE,&m_pStream);
OleLoadPicture(m_pStream,filesize,TRUE,IID_IPicture,(LPVOID*)&m_pPicture);
m_pPicture->get_Width(&m_JpgWidth);
m_pPicture->get_Height(&m_JpgHeight);
CDC *pDC = m_Picture.GetDC();
CDC dc;
dc.CreateCompatibleDC(pDC);
CRect rect;
m_Picture.GetClientRect(&rect);
m_pPicture->Render(pDC->m_hDC,rect.left,rect.top,rect.Width(),rect.Height(),0,m_JpgHeight,m_JpgWidth,-m_JpgHeight,NULL);
GlobalFree(hMem);
DeleteDC(dc);
}
这是读取图片路径读取BMP或JPG图像。并显示
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询