加载图片的方法(I)

1.  通过GDI+加载(加载图片 + 显示图片)

(1)先加载图片方法:

//方法I
//CString转WCHAR(因为Image构造函数参数为宽字符)
int l = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)sFile, -1, NULL, 0);
wchar_t *w = new wchar_t[l];
memset(w, 0, l);
INT N = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)sFile, -1, w, l);
Image image(w);
delete[]w;

//方法II
CT2CW strFileName(sFile);
Image image(strFileName);

//方法III
CT2CW strFileName(sFile);
Image *pImage = Image::FromFile(strFileName);

(2)显示图片方法 --- 通常需要在重载方法 OnEraseBkgnd() 中来画图片

//方法I..图片直接显示到窗体上
HDC hDC = ::GetDC(m_hWnd);
Graphics graphics(hDC);
graphics.DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());
//或graphics.DrawImage(pImage, 0, 0, pImage->GetWidth(), pImage->GetHeight());

//方法II..图片直接显示到窗体上
CDC *pDC = GetDC();
Graphics graphics(pDC->m_hDC);
graphics.DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());

//方法III..图片直接显示到窗体上
CClientDC dc(this);
Graphics graphics(dc.m_hDC); //通过设备描述表句柄实例化一个 gdi+ Graphics 类成员
graphics.DrawImage(pImgBmp, 0, 0)

//方法IV..图片显示到PictureCtrl控件上
CClientDC *pDC = new CClientDC(GetDlgItem(IDC_STATIC_PIC)); //注意CWindowDC,CClientDC,CPaintDC的区别
Graphics graphics(pDC->m_hDC);
graphics.DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());
delete pDC;

例如:

void CTestDlg::LoadPng()
{
    CDC *pDC = GetDC();
    Graphics graphics(pDC->m_hDC);
    
    CT2CW strFileName(sFile);
    Image image(strFileName);
    graphics.DrawImage(&image, 10,10, image.GetWidth(), image.GetHeight());
}


2. 通过CBitmap加载窗体背景

//先添加并初始化成员变量
CBitmap m_BackBmp;  
m_BackBmp.LoadBitmap(IDB_BITMAP1);

//再背景窗体重绘
BOOL CConnectDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值

	CDialogEx::OnEraseBkgnd(pDC);

	//窗体背景图片
	CRect rcClient;
	GetClientRect(&rcClient);

	BITMAP bm;
	m_BackBmp.GetBitmap(&bm);

	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap *pOldBmp = memDC.SelectObject(&m_BackBmp);
	pDC->StretchBlt(0, 0, rcClient.Width(), rcClient.Height(), &memDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
	memDC.SelectObject(pOldBmp);
	memDC.DeleteDC();

	return TRUE;
}


2. 通过Cimage加载图片

//用于擦出背景 (场景:OnTimer()中定时擦出背景,OnEraseBkgnd()重绘背景,可实现图片的移动动画效果)
<span style="font-family: Arial, Helvetica, sans-serif;">HWND hwnd = GetSafeHwnd(); //获取窗口的HWND</span>
<span style="font-family: Arial, Helvetica, sans-serif;">::InvalidateRect( hwnd, NULL, true ); //或者 ::InvalidateRect( hwnd, NULL, false );</span>
<span style="font-family: Arial, Helvetica, sans-serif;">::UpdateWindow(hwnd);</span>

CDC *pDC = GetDC();
CImage Image;
Image.Load(strPath);
if (Image.IsNull())
{
    MessageBox(_T("没加载成功"));
    return -1;
}
if (Image.GetBPP() == 32) //确认该图像包含Alpha通道
{
    int i;
    int j;
    for (i=0; i<Image.GetWidth(); i++)
    {
        for (j=0; j<Image.GetHeight(); j++)
        {
            byte *pByte = (byte *)Image.GetPixelAddress(i, j);
            pByte[0] = pByte[0] * pByte[3] / 255;
            pByte[1] = pByte[1] * pByte[3] / 255;
            pByte[2] = pByte[2] * pByte[3] / 255;
        }
    }
}
Image.Draw(pDC->m_hDC,0,0);
Image.Destroy();
ReleaseDC(pDC);

3. 加载.ico

(1).使用函数 LoadIcon() 载入图标。因为LoadIcon() 是类 CWinApp 的成员函数,同时函数 LoadIcon() 返回所载入图标的句柄。所以我们采用以下方法来调用函数 LoadIcon():

//加载ICO图片  
HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);  
m_btnCannel.SetIcon(hIcon);  //或 ((CButton*)GetDlgItem(IDC_BUTTON1))->SetIcon(m_hicn1);  

但是这种方法并不能加载系统预定义图标,在CWinApp中还有个装载图标的函数,

HICON LoadStandardIcon( LPCTSTR lpszIconName ) const;
如:
AfxGetApp()->LoadStandardIcon(IDI_QUESTION);

(2)全局::LoadIcon(), ::SetIcon()

当然在MFC中可以调用全局函数来加载图标,但是如果要装载自己添加的图标资源,要注意下面几种写法:

HICON hIcon = ::LoadIcon(AfxGetApp()->m_hInstance,(LPCTSTR)IDI_ICON1);
HICON hIcon = ::LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(ICON1));
HICON hIcon = ::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(ICON1));
::SetIcon(hIcon, FALSE);
或
::SetIcon(::LoadIcon(NULL, MAKEINTRESOURCE(ICON1)), FALSE);

以上都是正确的.由于添加的图标资源ID不是字符型的(在resource.h定义),所以要转换:
在前面加上LPCTSTR强制转换
用MAKEINTRESOURCE转换
它的定义如下:
#define MAKEINTRESOURCE(i) (LPCTSTR)((DWORD)((WORD)(i))),它的作用就是将资源标识转换为名称字符串


(3). 全局::LoadImage()

// Find correct resource handle
HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(IDI_ICON1), RT_GROUP_ICON);

// Set icon when the mouse is IN the button
HICON hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0, 0);


(4). 从文件加载

HICON hIconIn = (HICON)::LoadImage(NULL,"MyButtonBG.ico",IMAGE_ICON,100,150,LR_LOADFROMFILE);

4. 加载.bmp

//加载BMP图片  
CBitmap Bitmap;  
Bitmap.LoadBitmap(IDB_BITMAP1);  
HBITMAP hBitmap = (HBITMAP)Bitmap.Detach();  
m_btnCannel.SetBitmaps(hBitmap, RGB(255, 255, 255));  

你可能感兴趣的:(加载图片的方法(I))