The following example draws an image twice: once with no change and once with a transformation specified by a color matrix. The transformation increases all red channel values by 20 percent and increases all green channel values by 30 percent. The Matrix object (not a color matrix) passed to the DrawImage method specifies that the upper-left corner of the altered image is at (200, 20).
' ########################################################################################
' Microsoft Windows
' File: CGDIP_ColorMatrixEffectSetParameters.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10.02+, PBCC 6.02+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1
' // Header files for imported files
#INCLUDE ONCE "CWindow.inc" ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc" ' // CGdiPlus class
%IDC_GRCTX = 1001
' ========================================================================================
' The following example draws an image twice: once with no change and once with a
' transformation specified by a color matrix. The transformation increases all red channel
' values by 20 percent and increases all green channel values by 30 percent. The Matrix
' object (not a color matrix) passed to the DrawImage method specifies that the upper-left
' corner of the altered image is at (200, 20).
' ========================================================================================
SUB Example_ColorMatrixEffectSetParameters (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)
' Graphics graphics(hdc);
LOCAL graphics AS IGdipGraphics
graphics = pGdip.Graphics(hdc)
' Image myImage(L"Picture.bmp");
LOCAL myImage AS IGdipImage
myImage = pGdip.Image("climber.jpg")
' REAL srcWidth = (REAL)myImage.GetWidth();
' REAL srcHeight = (REAL)myImage.GetHeight();
LOCAL srcWidth AS SINGLE
srcWidth = myImage.GetWidth
LOCAL srcHeight AS SINGLE
srcHeight = myImage.GetHeight
' RectF srcRect(0.0f, 0.0f, srcWidth, srcHeight);
LOCAL srcRect AS RectF
srcRect = pGdip.RectF(0, 0, srcWidth, srcHeight)
' ColorMatrix myColMat = {
' 1.2f, 0.0f, 0.0f, 0.0f, 0.0f,
' 0.0f, 1.3f, 0.0f, 0.0f, 0.0f,
' 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
' 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
' 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
LOCAL myColMat AS ColorMatrix
myColMat.m(0, 0) = 1.2! : myColMat.m(0, 1) = 0.0! : myColMat.m(0, 2) = 0.0! : myColMat.m(0, 3) = 0.0! : myColMat.m(0, 4) = 0.0!
myColMat.m(1, 0) = 0.0! : myColMat.m(1, 1) = 1.3! : myColMat.m(1, 2) = 0.0! : myColMat.m(1, 3) = 0.0! : myColMat.m(1, 4) = 0.0!
myColMat.m(2, 0) = 0.0! : myColMat.m(2, 1) = 0.0! : myColMat.m(2, 2) = 1.0! : myColMat.m(2, 3) = 0.0! : myColMat.m(2, 4) = 0.0!
myColMat.m(3, 0) = 0.0! : myColMat.m(3, 1) = 0.0! : myColMat.m(3, 2) = 0.0! : myColMat.m(3, 3) = 1.0! : myColMat.m(3, 4) = 0.0!
myColMat.m(4, 0) = 0.0! : myColMat.m(3, 1) = 0.0! : myColMat.m(3, 2) = 0.0! : myColMat.m(3, 3) = 0.0! : myColMat.m(3, 4) = 1.0!
' Matrix myMatrix(1.0f, 0.0f, 0.0f, 1.0f, 200.0f, 20.0f);
LOCAL myMatrix AS IGdipMatrix
myMatrix = pGdip.Matrix
myMatrix.Matrix2(1, 0, 0, 1, 200, 20)
' ColorMatrixEffect myColMatEff;
' myColMatEff.SetParameters(&myColMat);
LOCAL myColMatEff AS IGdipColorMatrixEffect
myColMatEff = pGdip.ColorMatrixEffect
myColMatEff.SetParameters(myColMat)
' // Draw the image with no change.
' graphics.DrawImage(&myImage, 20.0, 20.0, srcWidth, srcHeight);
graphics.DrawImageRect(myImage, 20, 20, srcWidth, srcHeight)
' // Draw the transformed image.
' graphics.DrawImage(&myImage, &srcRect, &myMatrix, &myColMatEff, NULL, UnitPixel);
graphics.DrawImageFx(myImage, srcRect, myMatrix, myColMatEff, NOTHING, %UnitPixel)
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG
' // Create an instance of the CWindow class
LOCAL pWindow AS IWindow
pWindow = CLASS "CWindow"
IF ISNOTHING(pWindow) THEN EXIT FUNCTION
' // Create the main window
LOCAL hwnd AS DWORD
hwnd = pWindow.CreateWindow(%NULL, "ColorMatrixEffectSetParameters", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
pWindow.SetClientSize 400, 250
' // Center the window
pWindow.CenterWindow
' // Create an instance of the GdiPlus class
LOCAL pGdip AS IGdiPlus
pGdip = NewGdiPlus
' // Add a GDI+ aware graphic control
LOCAL hCtrl AS DWORD
hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
GraphCtx_Clear(hCtrl, %WHITE)
' // Get the memory device context of the graphic control
LOCAL hdc AS DWORD
hdc = GraphCtx_GetDc(hCtrl)
' // Draw the graphics
Example_ColorMatrixEffectSetParameters(pGdip, hdc)
' // Default message pump (you can replace it with your own)
pWindow.DoEvents(nCmdShow)
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
SELECT CASE uMsg
CASE %WM_COMMAND
SELECT CASE LO(WORD, wParam)
CASE %IDCANCEL
' // If the Escape key has been pressed...
IF HI(WORD, wParam) = %BN_CLICKED THEN
' // ... close the application by sending a WM_CLOSE message
SendMessage hwnd, %WM_CLOSE, 0, 0
END IF
END SELECT
CASE %WM_DESTROY
' // End the application
PostQuitMessage 0
EXIT FUNCTION
END SELECT
' // Pass unprocessed messages to Windows
FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================