VC:添加图片
总体:对于普通的BMP\DIB格式的图片,GDI中提供了LoadImage()函数;对于PNP、JPG、GIF、等格式的文件VC++中提供了OleLoadPicture()函数。
一、
1、创建一个单文档应用程序,并在View类中添加函数、变量如下:
void LoadPicture();
LPPICTURE m_picture;
float size;
CString filename;
2、LoadPicture();函数代码如下:
void CTestView::LoadPicture()
//LPPICTRUE LoadPicture(CStirng filename)
{
HANDLE hfile=CreateFile(filename,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
_ASSERTE(INVALID_HANDLE_VALUE!=hfile);
DWORD dfilesize=GetFileSize(hfile,NULL);
_ASSERTE(-1!=dfilesize);
LPVOID p=NULL;
HGLOBAL h=GlobalAlloc(GMEM_MOVEABLE,dfilesize);
_ASSERTE(NULL!=h);
p=GlobalLock(h);
_ASSERTE(NULL!=p);
DWORD dw=0;
BOOL bread=ReadFile(hfile,p,dfilesize,&dw,NULL);
_ASSERTE(FALSE!=bread);
GlobalUnlock(h);
CloseHandle(hfile);
LPSTREAM pstm=NULL;
HRESULT hr=CreateStreamOnHGlobal(h,TRUE,&pstm);
_ASSERTE(SUCCEEDED(hr)&&pstm);
if(m_picture)
m_picture->Release();
hr=::OleLoadPicture(pstm,dfilesize,FALSE,IID_IPicture,(LPVOID *)&m_picture);
_ASSERTE(SUCCEEDED(hr)&&m_picture);
pstm->Release();
//return m_picture;
}
3、在工具栏中添加两个按钮,扩大、缩小。在View类中,添加文件打开:
void CTestView::OnFileOpen()
{
// TODO: Add your command handler code here
static char sfile[]="JPEG(*.JPEG;*.JPE;*.JIF;*.JPG)|*.JPEG;*.JPE;*.JIF;*.JPG|BMP Windows位图(*.BMP;*.DIB;*.RLE)|*.BMP;*.DIB;*.RLE|PNG(*.PNG)|*.PNG||";
CFileDialog dlg(TRUE,"jpg"," ",OFN_ALLOWMULTISELECT,sfile);
if(IDOK==dlg.DoModal())
{
filename=dlg.GetPathName();
LoadPicture();
}
Invalidate();
}
void CTestView::OnBig()
{
// TODO: Add your command handler code here
size=size*0.909;
Invalidate();
}
void CTestView::OnSmall()
{
// TODO: Add your command handler code here
size=size*1.1;
Invalidate();
}
4、编写OnDraw函数代码:
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(m_picture)
{
long width;
long height;
m_picture->get_Width(&width);
m_picture->get_Height(&height);
int nwidth=MulDiv(width,GetDeviceCaps(pDC->GetSafeHdc(),LOGPIXELSX),
2540);
int nheight=MulDiv(height,GetDeviceCaps(pDC->GetSafeHdc(),LOGPIXELSY),
2540);
CRect rc;
GetClientRect(&rc);
m_picture->Render(pDC->GetSafeHdc(),(rc.Width()-nwidth/size)/2,(rc.Height()-nheight/size)/2,
nwidth/size,nheight/size,0,height,width,-height,&rc);
}
}
二、
1、添加位图资源
2、函数如下:
void CTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);//得到窗体的大小
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);//加载背景图片
BITMAP bitMap;
bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);//画窗体
// CDialog::OnPaint();
}
}
三、
1、位图:
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);//得到窗体的大小
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);//加载背景图片
BITMAP bitMap;
bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
long width;
width=bitmap.bmWidth;
//m_picture->get_Width(&width);
long height;
height;=bitmap.bmHeight;
//m_picture->get_Height(&height);
COLORREF color1,color2;
for(long i=0;i<height/2;i++)
{
for(long j=0;j<width;j++)
{
color1=dcMem.GetPixel(j,i);
color2=dcMem.GetPixel(width-j,height-i);
dcMem.SetPixel(j,i,color2);
dcMem.SetPixel(width-j,height-i,color1);
}
}*/
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);//画窗体
2、图片:
/*CPaintDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
long width;
m_picture->get_Width(&width);
long height;
m_picture->get_Height(&height);
COLORREF color1,color2;
for(long i=0;i<height/2;i++)
{
for(long j=0;j<width;j++)
{
color1=dcMem.GetPixel(j,i);
color2=dcMem.GetPixel(width-j,height-i);
dcMem.SetPixel(j,i,color2);
dcMem.SetPixel(width-j,height-i,color1);
}
}*/