您的位置:首页 > 其它

Gdi+与双缓冲的图片显示(应用篇)

2015-09-24 10:49 417 查看
一、专词理解</span>

Gdi+:负责Windows绘图的API。

双缓冲:绘图操作和显示分开,绘制完成后,直接拷贝显示。

二、MFC处理流程

1.准备GDI+接口

包含头文件:

#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")


构造函数里GDI+初始化(容易遗忘掉的地方):
CImageProcessingView::CImageProcessingView():
m_iImgHeight(0),
m_iImgWidth(0)
{
// TODO:  在此处添加构造代码
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
}


</pre><pre>

2.构建双缓冲

private:
CBitmap m_bitmapMemory;// 内存位图
CDC     m_dcMemory;    // 内存dc

int m_iImgHeight;// 图片高
int m_iImgWidth;// 图片宽

// 操作
public:
void CreateMemoryDC();  // 创建位图和内存dc
void ReleaseMemoryDC();    // 释放位图和内存dc
void CImageProcessingView::CreateMemoryDC()
{
CImageProcessingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

wchar_t strImg[1024];
MultiByteToWideChar(CP_ACP, NULL, pDoc->GetImagePath().c_str(),\
1024, strImg, 1024);
Image img(strImg);
m_iImgWidth = img.GetWidth();
m_iImgHeight = img.GetHeight();

CClientDC dc(this);
m_dcMemory.CreateCompatibleDC(&dc);
m_bitmapMemory.CreateCompatibleBitmap(&dc, m_iImgWidth, m_iImgHeight);
SelectObject(m_dcMemory, m_bitmapMemory);

// 绘图
Graphics graphics(m_dcMemory);
graphics.DrawImage(&img, 0, 0);
}

void CImageProcessingView::ReleaseMemoryDC()
{
m_bitmapMemory.DeleteObject();
m_dcMemory.DeleteDC();
}


3. OnDraw里绘图

void CImageProcessingView::OnDraw(CDC* pDC)
{
CImageProcessingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

if (pDoc->GetImagePath() == "")
return;

// 获取客户区大小
CRect rect;
GetClientRect(&rect);

int nWidth = m_iImgWidth > rect.Width() ? m_iImgWidth : rect.Width();
int nHeight = m_iImgHeight > rect.Height() ? m_iImgHeight : rect.Height();

// 滚动窗口
CSize sizeTotal;
sizeTotal.cx = nWidth;
sizeTotal.cy = nHeight;
SetScrollSizes(MM_TEXT, sizeTotal);

pDC->BitBlt(0, 0, nWidth, nHeight, &m_dcMemory, 0, 0, SRCCOPY);
}

三、效果及源代码



代码地址:https://github.com/lk547256398/ImageProcessing
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  双缓冲 GDI+ MFC 图像