博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
再学 GDI+[100]: TGPImage(20) - 替换颜色
阅读量:5882 次
发布时间:2019-06-19

本文共 3027 字,大约阅读时间需要 10 分钟。

  hot3.png

本例效果图:
26153002_6PS3.gif

代码文件:

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, TeCanvas;type  TForm1 = class(TForm)    ButtonColor1: TButtonColor;    Button1: TButton;    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure FormPaint(Sender: TObject);    procedure ButtonColor1Click(Sender: TObject);    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure Button1Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}uses GDIPOBJ, GDIPAPI;var  img: TGPImage;  ImageAttributes: TGPImageAttributes;  ColorMap: array[0..0] of TColorMap; {每个元素包含新旧两种颜色}procedure TForm1.FormCreate(Sender: TObject);begin  img := TGPImage.Create('c:\temp\test.png');  ClientWidth := img.GetWidth;  ClientHeight := img.GetHeight;  ImageAttributes := TGPImageAttributes.Create;  ColorMap[0].oldColor := aclBlack;  ColorMap[0].newColor := aclBlack;  ButtonColor1.Caption := '替换';  Button1.Caption := '复原';end;procedure TForm1.FormDestroy(Sender: TObject);begin  img.Free;  ImageAttributes.Free;end;procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var  color: TColor;begin  color := GetPixel(Canvas.Handle, X, Y);  ColorMap[0].oldColor := ColorRefToARGB(color);  ButtonColor1.SymbolColor := color;  ButtonColor1.Refresh;end;procedure TForm1.FormPaint(Sender: TObject);var  g: TGPGraphics;begin  g := TGPGraphics.Create(Canvas.Handle);  g.DrawImage(img,              MakeRect(0, 0, img.GetWidth, img.GetHeight),              0, 0,              img.GetWidth, img.GetHeight,                               UnitPixel,              ImageAttributes              );  g.Free;end;procedure TForm1.Button1Click(Sender: TObject);begin  ButtonColor1.SymbolColor := ARGBToColorRef(ColorMap[0].OldColor);  ButtonColor1.Enabled := True;  ButtonColor1.Update;  ImageAttributes.ClearRemapTable; {ClearRemapTable}  Repaint;end;procedure TForm1.ButtonColor1Click(Sender: TObject);begin  ColorMap[0].newColor := ColorRefToARGB(ButtonColor1.SymbolColor);  ImageAttributes.SetRemapTable(Length(ColorMap), @ColorMap); {SetRemapTable}  Repaint;  ButtonColor1.Enabled := False;end;end.
窗体文件:

object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 220  ClientWidth = 315  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -11  Font.Name = 'Tahoma'  Font.Style = []  OldCreateOrder = False  Position = poDesktopCenter  OnCreate = FormCreate  OnDestroy = FormDestroy  OnMouseUp = FormMouseUp  OnPaint = FormPaint  PixelsPerInch = 96  TextHeight = 13  object ButtonColor1: TButtonColor    Left = 231    Top = 8    Caption = 'ButtonColor1'    TabOrder = 0    OnClick = ButtonColor1Click  end  object Button1: TButton    Left = 231    Top = 39    Width = 75    Height = 25    Caption = 'Button1'    TabOrder = 1    OnClick = Button1Click  endend

转载于:https://my.oschina.net/hermer/blog/319113

你可能感兴趣的文章
CentOS图形界面和命令行切换
查看>>
HTML5通信机制与html5地理信息定位(gps)
查看>>
汽车常识全面介绍 - 悬挂系统
查看>>
加快ALTER TABLE 操作速度
查看>>
学习笔记之软考数据库系统工程师教程(第一版)
查看>>
PHP 程序员的技术成长规划
查看>>
memcached 分布式聚类算法
查看>>
jquery css3问卷答题卡翻页动画效果
查看>>
$digest already in progress 解决办法——续
查看>>
虚拟机 centos设置代理上网
查看>>
Struts2中Date日期转换的问题
查看>>
mysql 数据类型
查看>>
Ubuntu 设置当前用户sudo免密码
查看>>
设置tomcat远程debug
查看>>
android 电池(一):锂电池基本原理篇【转】
查看>>
Total Command 常用快捷键
查看>>
ionic 调用手机的打电话功能
查看>>
怎么使用阿里云直播服务应用到现在主流直播平台中
查看>>
1000 加密算法
查看>>
exif_imagetype() 函数在linux下的php中不存在
查看>>