數位影像處理 GDI+環境的初始化

來源:互聯網
上載者:User

標籤:影像處理   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.