標籤:影像處理 gdi+
註:如果包含<gdiplus.h> 後出現編譯錯誤,那麼看看stdafx.h中有沒有定義WIN32_LEAN_AND_MEAN,有的話注釋掉就OK
【1】添加檔案頭與庫
#include <gdiplus.h>using namespace Gdiplus;#pragma comment(lib, "GdiPlus.lib")
【2】添加有效成員(保證使用gdi+的地方都有效)
ULONG_PTR m_gdiplusToken;
【3】初始化gdi+資源(使用gdi+之前)
Gdiplus::GdiplusStartupInput StartupInput; GdiplusStartup(&m_gdiplusToken,&StartupInput,NULL);
【4】銷毀gdi+資源(使用gdi+之後)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
【5】測試樣本(初始化與銷毀工作已經放到應用程式的初始化與退出中
)
Gdiplus::Graphics graphics(hDC);Gdiplus::SolidBrush solidBrush(Gdiplus::Color::Red); Gdiplus::FontFamily fontFamily(L"宋體");Gdiplus::Font font(&fontFamily, 16, FontStyleRegular, UnitPoint);graphics.DrawString(L"GDI+程式示意", -1, &font, Gdiplus::PointF(0, 0), &solidBrush);graphics.ReleaseHDC(hDC);
【6】為了方便使用,將初始化代碼和銷毀代碼放到一個類中,使用時只需要定義這個類的一個global執行個體就可以了 (如果使用gdi+的地方比較多,建議將初始化放到主應用程式的初始化中,提高效率,而不是api中
)
class GdiPlusIniter{public: GdiPlusIniter(){ Gdiplus::GdiplusStartupInput StartupInput; GdiplusStartup(&m_gdiplusToken,&StartupInput,NULL); } ~GdiPlusIniter(){ Gdiplus::GdiplusShutdown(m_gdiplusToken); }private: ULONG_PTR m_gdiplusToken; };
在DLL中使用GDI+庫,只需要包含GdiPlus.h和GdiPlus.lib,初始化GDI+環境的工作只需要在主調用程式做,否則在DLL初始化代碼中初始化GDI+環境容易發生DLL重入的錯誤,而且頻繁的切換效率低。
參考文章:
http://www.cnblogs.com/hdtianfu/archive/2013/05/10/3071479.html