Learn more about GDI + [91]: tgpimage (11)-grayscale conversion Image

Source: Internet
Author: User
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

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.