原来见过一个png图像换肤处理的文章,但其中颜色处理上有些问题,特作优化修正处理,本是回复群友的问题的,所以用的是bmp图片,png图片处理原理是一样的。代码如下:
unit
Unit1
;
interface
uses
Windows , Messages , SysUtils , Variants , Classes , Graphics , Controls , Forms ,
Dialogs , StdCtrls , ExtCtrls ;
type
TForm1 = class ( TForm )
ColorBox1 : TColorBox ;
procedure btn1Click ( Sender : TObject );
procedure ColorBox1Select ( Sender : TObject );
procedure FormPaint ( Sender : TObject );
private
{ Private declarations }
public
{ Public declarations }
end ;
var
Form1 : TForm1 ;
implementation
{$R *.dfm}
procedure TForm1 . btn1Click ( Sender : TObject );
var
i , j : Integer ;
Row : pRGBTriple ;
Rgb : Longint ;
RGB_R , RGB_G , RGB_B : Byte ;
Gray : Byte ;
bmp : TBitmap ;
begin
RGB := ColorToRGB ( ColorBox1 . Selected );
RGB_R := GetRValue ( RGB );
RGB_G := GetGValue ( RGB );
RGB_B := GetBValue ( RGB );
bmp := TBitmap . Create ;
try
bmp . LoadFromFile ( 'E:/Documents and Settings/siow/桌面/main.bmp' );
for i := 0 to bmp . Height - 1 do
begin
Row := bmp . Scanline [ i ];
for j := 0 to bmp . Width - 1 do
begin
//灰度化
Gray := Trunc (( Row ^. rgbtRed * 306 + Row ^. rgbtGreen * 601 + Row ^. rgbtBlue * 117 + 512 ) shr 10 );
Row ^. rgbtRed := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_R )/ 255 );
Row ^. rgbtGreen := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_G )/ 255 );
Row ^. rgbtBlue := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_B )/ 255 );
Inc ( Row );
end ;
end ;
self . Canvas . StretchDraw ( bmp . Canvas . ClipRect , bmp );
finally
bmp . Free ;
end ;
end ;
procedure TForm1 . ColorBox1Select ( Sender : TObject );
begin
btn1Click ( nil );
end ;
procedure TForm1 . FormPaint ( Sender : TObject );
begin
btn1Click ( nil );
end ;
end .
interface
uses
Windows , Messages , SysUtils , Variants , Classes , Graphics , Controls , Forms ,
Dialogs , StdCtrls , ExtCtrls ;
type
TForm1 = class ( TForm )
ColorBox1 : TColorBox ;
procedure btn1Click ( Sender : TObject );
procedure ColorBox1Select ( Sender : TObject );
procedure FormPaint ( Sender : TObject );
private
{ Private declarations }
public
{ Public declarations }
end ;
var
Form1 : TForm1 ;
implementation
{$R *.dfm}
procedure TForm1 . btn1Click ( Sender : TObject );
var
i , j : Integer ;
Row : pRGBTriple ;
Rgb : Longint ;
RGB_R , RGB_G , RGB_B : Byte ;
Gray : Byte ;
bmp : TBitmap ;
begin
RGB := ColorToRGB ( ColorBox1 . Selected );
RGB_R := GetRValue ( RGB );
RGB_G := GetGValue ( RGB );
RGB_B := GetBValue ( RGB );
bmp := TBitmap . Create ;
try
bmp . LoadFromFile ( 'E:/Documents and Settings/siow/桌面/main.bmp' );
for i := 0 to bmp . Height - 1 do
begin
Row := bmp . Scanline [ i ];
for j := 0 to bmp . Width - 1 do
begin
//灰度化
Gray := Trunc (( Row ^. rgbtRed * 306 + Row ^. rgbtGreen * 601 + Row ^. rgbtBlue * 117 + 512 ) shr 10 );
Row ^. rgbtRed := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_R )/ 255 );
Row ^. rgbtGreen := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_G )/ 255 );
Row ^. rgbtBlue := Round ( 255 - ( 255 - Gray )* ( 255 - RGB_B )/ 255 );
Inc ( Row );
end ;
end ;
self . Canvas . StretchDraw ( bmp . Canvas . ClipRect , bmp );
finally
bmp . Free ;
end ;
end ;
procedure TForm1 . ColorBox1Select ( Sender : TObject );
begin
btn1Click ( nil );
end ;
procedure TForm1 . FormPaint ( Sender : TObject );
begin
btn1Click ( nil );
end ;
end .