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

来源:互联网 发布:淘宝直播中控台网址 编辑:程序博客网 时间:2024/05/01 08:55

一、专词理解</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;    // 内存dcint m_iImgHeight;// 图片高int m_iImgWidth;// 图片宽// 操作public:void CreateMemoryDC();  // 创建位图和内存dcvoid 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);}

三、效果及源代码


代码地址:

0 0