Delphi图像处理之图像亮度处理

------开发环境Delphi7

-----效果图:
Delphi图像处理之图像亮度处理

-----Unit开始
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtDlgs, ExtCtrls, ComCtrls, Math;

type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
OpenPictureDialog1: TOpenPictureDialog;
Button1: TButton;
Label1: TLabel;
TrackBar1: TTrackBar;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
private
procedure SetImageBrightness(sValue: Byte=0);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
Label1.Caption:='图片宽x高:'+inttostr(Image1.Picture.Width)+'x'+inttostr(Image1.Picture.Height);
end;
end;

procedure TForm1.SetImageBrightness(sValue: Byte=0);
var
vP:PByteArray;
x,y:Integer;
vBmp:TBitmap;
vGray:Integer;
begin
if Image1.Picture.Graphic =nil then
begin
ShowMessage('没有图片!');
Exit;
end;
//亮度调节就是,RBG三个分量同时加减一个值
vBmp:=TBitmap.Create;
vBmp.Assign(Image1.Picture.Bitmap);
vBmp.PixelFormat:=pf24bit;
for y:=0 to vBmp.Height-1 do
begin
vP:=vBmp.ScanLine[y];
for x:=0 to vBmp.Width-1 do
begin
vP[3x+2]:=Min(255,vP[3x+2]+sValue);
vP[3x+1]:=Min(255,vP[3x+1]+sValue);
vP[3x]:=Min(255,vP[3x]+sValue);
end;
end;
Image2.Picture.Assign(vBmp);
vBmp.Free;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
SetImageBrightness(TrackBar1.Position);
Label2.Caption:='亮度:'+inttostr(TrackBar1.Position);
end;

end.

-----Unit结束

-----Form开始
object Form1: TForm1
Left = 278
Top = 271
Width = 944
Height = 563
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 8
Top = 16
Width = 441
Height = 377
Center = True
Proportional = True
Stretch = True
end
object Image2: TImage
Left = 464
Top = 16
Width = 457
Height = 377
Center = True
Proportional = True
Stretch = True
end
object Label1: TLabel
Left = 16
Top = 400
Width = 393
Height = 17
AutoSize = False
Caption = '图片宽x高:'
end
object Label2: TLabel
Left = 360
Top = 456
Width = 201
Height = 17
AutoSize = False
end
object Button1: TButton
Left = 104
Top = 432
Width = 161
Height = 25
Caption = 'Button1_加载图片'
TabOrder = 0
OnClick = Button1Click
end
object TrackBar1: TTrackBar
Left = 8
Top = 480
Width = 913
Height = 33
Ctl3D = True
LineSize = 0
Max = 255
ParentCtl3D = False
PageSize = 0
TabOrder = 1
OnChange = TrackBar1Change
end
object OpenPictureDialog1: TOpenPictureDialog
Filter = 'Bitmaps (.bmp)|.bmp'
Left = 80
Top = 416
end
end

------Form结束

上一篇:RAD Studio 10.3.3 中的 Delphi Android 64 位支持


下一篇:Delphi中record 的运算符重载