本文例子展示GDI+的ColorMatrix的几种经典应用:
#include "../../SampleCode/comcode/Application.h" #pragma hdrstop #define ID_REASE 100 #define ID_ALPHA 101 #define ID_BRIGHT 102 #define ID_GRAY 103 #define ID_TWO 104 #define ID_INVERT 105 PGpBitmap bitmap; PGpImageAttr attr; Rect dRect; HFONT hFont = NULL; void CreateButton(LPTSTR caption, INT top, INT itemID, HFONT hFont) { HWND hButton = CreateWindow(TEXT("Button"), caption, WS_CHILD | WS_VISIBLE, 488, top, 80, 25, Handle, (HMENU)itemID, hInstance, NULL); SendMessage(hButton, WM_SETFONT,(WPARAM)(hFont), 0); } void OnCreate(void) { hFont = CreateFont(14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DECORATIVE, TEXT("宋体")); CreateButton(TEXT("半透明"), 30, ID_ALPHA, hFont); CreateButton(TEXT("亮度20%"), 65, ID_BRIGHT, hFont); CreateButton(TEXT("灰度化"), 100, ID_GRAY, hFont); CreateButton(TEXT("二值化"), 135, ID_TWO, hFont); CreateButton(TEXT("反 色"), 170, ID_INVERT, hFont); CreateButton(TEXT("重 置"), 205, ID_REASE, hFont); bitmap = BitmapFromFile(L"..//..//Media//Source.jpg", FALSE); attr = ImageAttrCreate(); dRect = MakeRect(0, 0, ImageGetWidth(bitmap), ImageGetHeight(bitmap)); } void OnDestroy(void) { if (hFont) DeleteObject(hFont); ImageDelete(bitmap); ImageAttrDelete(attr); } void OnCommand(WORD NotifyCode, WORD itemID, HWND ctlHandle) { ColorMatrix matrix; INT i; memset(&matrix, 0, sizeof(ColorMatrix)); for (i = 0; i < 5; i ++) matrix.m[i][i] = 1.0f; ImageAttrReset(attr, ColorAdjustTypeDefault); switch (itemID) { case ID_REASE: InvalidateRect(Handle, &dRect, FALSE); return; case ID_ALPHA: matrix.m[3][3] = 0.5f; break; case ID_BRIGHT: matrix.m[4][0] = matrix.m[4][1] = matrix.m[4][2] = 0.2f; break; case ID_GRAY: case ID_TWO: matrix.m[0][0] = matrix.m[0][1] = matrix.m[0][2] = 0.30f; matrix.m[1][0] = matrix.m[1][1] = matrix.m[1][2] = 0.59f; matrix.m[2][0] = matrix.m[2][1] = matrix.m[2][2] = 0.11f; break; case ID_INVERT: matrix.m[0][0] = matrix.m[1][1] = matrix.m[2][2] = -1.0f; break; } ImageAttrSetColorMatrix(attr, &matrix, ColorMatrixFlagsDefault, ColorAdjustTypeDefault); if (itemID == ID_TWO) ImageAttrSetThreshold(attr, 0.5, ColorAdjustTypeDefault); InvalidateRect(Handle, &dRect, FALSE); } void OnPaint(HDC DC) { PGpGraphics g = GraphicsCreate(DC); // GDI+ 画布 GraphicsDrawImageRectRect(g, bitmap, &dRect, 0, 0, dRect.Width, dRect.Height, UnitPixel, attr); GraphicsDelete(g); } WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 这里使用了双缓冲画窗口 */ InitApplication(hInstance, nCmdShow, TRUE); CreateProc = OnCreate; DestroyProc = OnDestroy; PaintProc = OnPaint; CommandProc = OnCommand; return RunApplication(TEXT("C语言Gdiplus演示例子 -- ColorMatrix"), 582, 392); } //---------------------------------------------------------------------------
例子代码使用的窗口框架代码和GDI+ C语言版本下载地址见《在C语言Windows应用程序中使用GDI+》。
下面是例子运行界面图:
有关GDI+ ColorMatrix更多的内容可参见《GDI+ ColorMatrix的完全揭秘》和《GDI+ for VCL基础 -- 颜色调整矩阵ColorMatrix详解》。
指导和建议请来信:[email protected],[email protected]