MFC中Picture Control控件显示照片的几种方式

目前使用CImage和CBitmap两个类,还有是将CImage转CBitmap显示。

MFC界面拖拽一个button按钮和一个Picture Control控件。

1.CImage显示。这种方式显示图片会有颜色不对的情况

复制代码
void Cpicture_test_controlDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码

	CImage image;
	HRESULT hResult = image.Load("D:\\jpeg.bmp");
	if (!SUCCEEDED(hResult)) {
		AfxMessageBox("imread error");
	}
	//获取picture control 句柄
	CStatic *pStatic = (CStatic*)GetDlgItem(IDC_STATIC_PC);
	
	//获取控件大小
	CRect rect;
	pStatic->GetClientRect(&rect);
	int controlWidth = rect.Width();
	int controlHeight = rect.Height();

	//获取图片原始尺寸
	int imageWidth = image.GetWidth();
	int imageHeight = image.GetHeight();

	//创建兼容的DC
	CDC *pDC = pStatic->GetDC();
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);

	//创建兼容的位图,并选入内存DC
	CBitmap bmp;
	bmp.CreateCompatibleBitmap(pDC, controlWidth, controlHeight);
	CBitmap* pOldbmp = memDC.SelectObject(&bmp);

	BITMAP bmpInfo;
	bmp.GetBitmap(&bmpInfo); // 获取位图信息

	//清空背景
	memDC.FillSolidRect(&rect, pDC->GetBkColor());

	//使用CImage绘制倒内存DC, 这里使用缩放
	image.Draw(memDC.m_hDC, 0, 0, controlWidth, controlHeight, 0, 0, imageWidth, imageHeight);

	//将内存DC绘制到控件上
	pDC->BitBlt(0, 0, controlWidth, controlHeight, &memDC, 0, 0, SRCCOPY);

	//清理
	memDC.SelectObject(pOldbmp);
	ReleaseDC(pDC);
}

2.CBitmap显示

复制代码
void Cpicture_test_controlDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码

	//需要再资源视图中增加资源,资源类型为Bitmap
	CBitmap img;
	if (!img.LoadBitmap(IDB_BITMAP2)) {
		AfxMessageBox("imread error");
	}

	// 获取Picture Control的设备上下文
	CStatic *pStatic = (CStatic*)GetDlgItem(IDC_STATIC_PC);
	CDC *pDC = pStatic->GetDC();

	// 获取对话框和图片的尺寸
	CRect rect;
	pStatic->GetClientRect(&rect);
	BITMAP bmpInfo;
	img.GetBitmap(&bmpInfo);

	// 创建兼容的内存设备上下文
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = memDC.SelectObject(&img);

	// 绘制缩放后的图片
	int iStretchMode = pDC->SetStretchBltMode(COLORONCOLOR); // 色彩保留
	pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
	pDC->SetStretchBltMode(iStretchMode);

	// 清理
	memDC.SelectObject(pOldBitmap);
	ReleaseDC(pDC);
}

3.将CImage转CBitmap显示。有两种方式

复制代码
void Cpicture_test_controlDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码

	CImage image;
	HRESULT hResult = image.Load("D:\\jpeg.bmp");
	if (!SUCCEEDED(hResult)) {
		AfxMessageBox("imread error");
	}
	HBITMAP hBitmap = image.Detach();
	CBitmap img;
	img.DeleteObject();
	img.Attach(hBitmap);

	// 获取Picture Control的设备上下文
	CStatic *pStatic = (CStatic*)GetDlgItem(IDC_STATIC_PC);
	CDC *pDC = pStatic->GetDC();

	// 获取对话框和图片的尺寸
	CRect rect;
	pStatic->GetClientRect(&rect);
	BITMAP bmpInfo;
	img.GetBitmap(&bmpInfo);

	// 创建兼容的内存设备上下文
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = memDC.SelectObject(&img);

	// 绘制缩放后的图片
	int iStretchMode = pDC->SetStretchBltMode(COLORONCOLOR); // 色彩保留
	pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
	pDC->SetStretchBltMode(iStretchMode);

	// 清理
	memDC.SelectObject(pOldBitmap);
	ReleaseDC(pDC);

}

void Cpicture_test_controlDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码

	CImage image;
	HRESULT hResult = image.Load("D:\\jpeg.bmp");
	if (!SUCCEEDED(hResult)) {
		AfxMessageBox("imread error");
	}
	HBITMAP hBitmap = image.Detach();
	CBitmap *img = CBitmap::FromHandle(hBitmap);

	// 获取Picture Control的设备上下文
	CStatic *pStatic = (CStatic*)GetDlgItem(IDC_STATIC_PC);
	CDC *pDC = pStatic->GetDC();

	// 获取对话框和图片的尺寸
	CRect rect;
	pStatic->GetClientRect(&rect);
	BITMAP bmpInfo;
	img->GetBitmap(&bmpInfo);

	// 创建兼容的内存设备上下文
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = memDC.SelectObject(img);

	// 绘制缩放后的图片
	int iStretchMode = pDC->SetStretchBltMode(COLORONCOLOR); // 色彩保留
	pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
	pDC->SetStretchBltMode(iStretchMode);

	// 清理
	memDC.SelectObject(pOldBitmap);
	ReleaseDC(pDC);

}
相关推荐
_Narcissus_1 分钟前
动态规划初探(含完整代码实现)
c语言·数据结构·c++·算法·动态规划·背包问题·最短路径
法哥20121 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++
_Narcissus_2 小时前
枚举和模拟算法笔记
c语言·数据结构·c++·笔记·算法·模拟·枚举
冻柠檬飞冰走茶2 小时前
《数据结构实验指导-C++语言版》 在顺序表 list 中查找元素 x
开发语言·数据结构·c++·算法·list
Brilliantwxx2 小时前
【C++】 高阶数据结构图(1)并查集
开发语言·数据结构·c++
小鱼仙官3 小时前
Ubuntu C++ NTP校时程序
linux·c++·ubuntu
冻柠檬飞冰走茶3 小时前
《数据结构实验指导-C++语言版》 返回单链表 list 中第 i 个元素值
开发语言·数据结构·c++·算法·list
老当益壮梁奶奶3 小时前
Linux软件编程学习笔记(二)Linux文件IO编程入门:从fopen到fgetc
linux·c语言·c++·笔记·学习
雪的季节3 小时前
C++ 高级(泛型编程与模板)(有空再研究吧)
c++
三十岁老牛再出发3 小时前
8月21日总结
c++·python