--------开发环境Delphi7
----效果图:
------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;
Label1: TLabel;
OpenPictureDialog1: TOpenPictureDialog;
Button1: TButton;
TrackBar1: TTrackBar;
Label2: TLabel;
procedure TrackBar1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure SetImageContrast(sValue: Byte);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SetImageContrast(sValue: Byte);
const
vThresholdValue:Byte=128;
var
vP:PByteArray;
x,y:Integer;
vBmp:TBitmap;
vGray:Integer;
begin
//对比度就是让暗的像素越暗,让亮的像素越亮;默认一个阈值,大于此阈值的让它加上某个值更亮,小于阈值的减去某个值让它越暗
if Image1.Picture.Graphic =nil then
begin
Label2.Caption:='请加载图片!';
Exit;
end;
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
if (vP[3x+2]>vThresholdValue) and (vP[3x+2]<255)
and (vP[3x+1]>vThresholdValue) and (vP[3x+1]<255)
and (vP[3x]>vThresholdValue) and (vP[3x]<255) then
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;
if (vP[3*x+2]>0) and (vP[3*x+2]<vThresholdValue)
and (vP[3*x+1]>0) and (vP[3*x+1]<vThresholdValue)
and (vP[3*x]>0) and (vP[3*x]<vThresholdValue) then
begin
vP[3*x+2]:=Max(0,vP[3*x+2]-sValue);
vP[3*x+1]:=Max(0,vP[3*x+1]-sValue);
vP[3*x]:=Max(0,vP[3*x]-sValue);
end;
end;
end;
Image2.Picture.Assign(vBmp);
vBmp.Free;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
SetImageContrast(TrackBar1.Position);
Label2.Caption:='对比度:'+Inttostr(TrackBar1.Position);
end;
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.FormCreate(Sender: TObject);
begin
OpenPictureDialog1.Filter:='Bitmaps (.bmp)|.bmp';
self.DoubleBuffered:=true;
end;
end.
------Unit结束
------Form开始
object Form1: TForm1
Left = 364
Top = 187
BorderStyle = bsDialog
Caption = 'Form1'
ClientHeight = 485
ClientWidth = 886
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 8
Top = 16
Width = 425
Height = 337
Center = True
Proportional = True
Stretch = True
end
object Image2: TImage
Left = 448
Top = 16
Width = 425
Height = 337
Center = True
Proportional = True
Stretch = True
end
object Label1: TLabel
Left = 16
Top = 360
Width = 385
Height = 25
AutoSize = False
Caption = '图片宽x高:'
end
object Label2: TLabel
Left = 272
Top = 424
Width = 297
Height = 25
Alignment = taCenter
AutoSize = False
end
object Button1: TButton
Left = 16
Top = 416
Width = 161
Height = 25
Caption = 'Button1_加载图片'
TabOrder = 0
OnClick = Button1Click
end
object TrackBar1: TTrackBar
Left = 8
Top = 448
Width = 873
Height = 33
Max = 127
TabOrder = 1
OnChange = TrackBar1Change
end
object OpenPictureDialog1: TOpenPictureDialog
Filter = 'Bitmaps (.bmp)|.bmp'
Left = 72
Top = 368
end
end
--------Form结束