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);

}
相关推荐
Forget the Dream25 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
停止重构26 分钟前
【开源】低代码 C++程序框架,Linux多线程程序
c++·低代码·多线程·开源框架·云计算引擎
Duramentee29 分钟前
C++ 设计模式 十二:责任链模式 (读书 现代c++设计模式)
c++·设计模式·责任链模式
_GR34 分钟前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
萌の鱼1 小时前
leetcode 3183. 达到总和的方法数量
数据结构·c++·算法·leetcode
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
攻城狮7号4 小时前
【第十节】C++设计模式(结构型模式)-Flyweight( 享元)模式
c++·设计模式·享元模式
Forget the Dream4 小时前
设计模式之享元模式
java·c++·设计模式·享元模式
溟洵8 小时前
【C/C++】理解C++内存与Linux虚拟地址空间的关系---带你通透C++中所有数据
linux·c语言·c++
Chandler2410 小时前
C++11 智能指针:unique_ptr、shared_ptr和weak_ptr 功能特性 模拟实现
开发语言·c++