In this example:
Code File:
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) checkbox1: tcheckbox; Procedure formcreate (Sender: tobject); Procedure formpaint (Sender: tobject); Procedure checkbox1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses gdipobj, gdipapi; const {General mode} colormatrix1: tcolormatrix = (1.0, 0.0, 0.0, 0.0, 0.0), (0.0, 1.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0 )); {grayscale mode} colormatrix2: tcolormatrix = (0.3, 0.3, 0.3, 0.0, 0.0), (0.59, 0.59, 0.59, 0.0, 0.0), (0.11, 0.11, 0.11, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0, 0.0), (0.0, 0.0, 0.0, 0.0, 1.0); Procedure tform1.formcreate (Sender: tobject ); begin checkbox1.caption: = 'convert to grayscale '; end; Procedure convert (Sender: tobject); var G: tgpgraphics; IMG: tgpimage; imageattributes: tgpimageattributes; begin G: = tgpgraphics. create (canvas. handle); IMG: = tgpimage. create ('C: \ temp \ test.png '); imageattributes: = tgpimageattributes. create; If checkbox1.checked then imageattributes. setcolormatrix (colormatrix2) else imageattributes. setcolormatrix (colormatrix1); G. drawimage (IMG, makerect (4, 4, IMG. getwidth, IMG. getheight), 0, 0, IMG. getwidth, IMG. getheight, unitpixel, imageattributes); imageattributes. free; IMG. free; G. free; end; Procedure tform1.checkbox1click (Sender: tobject); begin repaint; end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 216 clientwidth = 269 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false position = pow.topcenter oncreate = formcreate onpaint = formpaint pixelsperinch = 96 textheight = 13 object checkbox1: tcheckbox left = 183 Top = 191 width = 79 Height = 17 caption = 'checkbox1' taborder = 0 onclick = checkbox1click endend