LeadTools创建“调整亮度和对比度”程序步骤

2026-02-27 21:12:41

1、打开Visual Studio .NET。点击 文件->新建->项目…。打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏闲海离中输入项目名称“ChangeBrightnessAndContrast”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。

2、在“解决方案资源管理器”中,右击“引用王秤”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32”,选择以下的DLL“:

Leadtools.dll

Leadtools.Codecs.dll

Leadtools.Codecs.Cmp.dll

Leadtools.ImageProcessing.Color.dll      

Leadtools.WinForms.dll 

3、从工具箱(视图->工具箱),添加8个讨畜RadioButton控件(将RadioButton的Text属性依照下表修改),两个Panel控件(Name分别修改为panelBefore和panelAfter)。如下图:

LeadTools创建“调整亮度和对比度”程序步骤

4、切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:

  1: using Leadtools;

  2: using Leadtools.Codecs;

  3: using Leadtools.WinForms;

  4: using Leadtools.ImageProcessing.Color;

5、将以下变量添加至Form1类:

  1: private RasterImageViewer beforePic;

  2: private RasterImageViewer afterPic;

  3: private RasterCodecs codecs;

  4: private RasterImage temp;  

6、添加Form1 Load事件句柄,在其中添加以下代码:

  1: beforePic = new RasterImageViewer();

  2: beforePic.BackColor = Color.DarkCyan;

  3: beforePic.Dock = DockStyle.Fill;

  4: beforePic.InteractiveMode = RasterViewerInteractiveMode.Pan;

  5: beforePic.HorizontalAlignMode = RasterPaintAlignMode.Center;

  6: beforePic.VerticalAlignMode = RasterPaintAlignMode.Center;

  7: beforePic.AutoResetScaleFactor = false;

  8: panelBefore.Controls.Add(beforePic);

  9: beforePic.BringToFront();

 10:

 11: afterPic = new RasterImageViewer();

 12: afterPic.BackColor = beforePic.BackColor;

 13: afterPic.Dock = beforePic.Dock;

 14: afterPic.InteractiveMode = beforePic.InteractiveMode;

 15: afterPic.HorizontalAlignMode = beforePic.HorizontalAlignMode;

 16: afterPic.VerticalAlignMode = beforePic.VerticalAlignMode;

 17: afterPic.AutoResetScaleFactor = beforePic.AutoResetScaleFactor;

 18: panelAfter.Controls.Add(afterPic);

 19: afterPic.BringToFront();

 20:

 21: codecs = new RasterCodecs();

 22: codecs.ThrowExceptionsOnInvalidImages = true;

 23: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image.jpg"));

7、双击radioButton1,在radioButton1 CheckedChanged事件句柄中添加以下代码:

(本段代码为ChangeIntensityCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: ChangeIntensityCommand command = new ChangeIntensityCommand();          

  3: command.Brightness = 250;

  4: command.Run(temp);

  5: codecs.Save(temp, Path.Combine(Application.StartupPath, @"..\..\Pic\ChangeIntensityCommandResult.jpg"), RasterImageFormat.Jpeg, 24);

  6: afterPic.Image = temp;

8、双击radioButton2,在radioButton2 CheckedChanged事件句柄中添加以下代码:

(本段代码为ChangeContrastCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: ChangeContrastCommand command = new ChangeContrastCommand();

  3:  

  4: command.Contrast = 250;

  5: command.Run(temp);

  6: afterPic.Image = temp;

9、双击radioButton3,在radioButton3 CheckedChanged事件句柄中添加以下代码:

(本段代码为GammaCorrectCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: GammaCorrectCommand command = new GammaCorrectCommand();

  3: 

  4: command.Gamma = 250;

  5: command.Run(temp);

  6: afterPic.Image = temp;

10、双击radioButton4,在radioButton4 CheckedChanged事件句柄中添加以下代码:

(本段代码为MultiplyCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: MultiplyCommand command = new MultiplyCommand();

  3: command.Factor = 151;

  4: 

  5: command.Run(temp);

  6: afterPic.Image = temp;

11、 双击radioButton5,在radioButton5 CheckedChanged事件句柄中添加以下代码:

(本段代码为ColorIntensityBalanceCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: ColorIntensityBalanceCommand command = new ColorIntensityBalanceCommand();

  3: ColorIntensityBalanceCommandData Shadows = new ColorIntensityBalanceCommandData();

  4: ColorIntensityBalanceCommandData MidTone = new ColorIntensityBalanceCommandData();

  5: ColorIntensityBalanceCommandData HighLight = new ColorIntensityBalanceCommandData();

  6:

  7: Shadows.Red = 60;

  8: Shadows.Blue = 0;

  9: Shadows.Green = 0;

 10:

 11: MidTone.Red = 40;

 12: MidTone.Blue = 0;

 13: MidTone.Green = 0;

 14:

 15: HighLight.Red = 70;

 16: HighLight.Blue = 0;

 17: HighLight.Green = 0;

 18:

 19: command.Shadows = Shadows;

 20: command.MidTone = MidTone;

 21: command.HighLight = HighLight;

 22: command.Luminance = false;

 23:

 24: command.Run(temp);

 25: afterPic.Image = temp;

12、双击radioButton6,在radioButton6 CheckedChanged事件句柄中添加以下代码:

(本段代码为ContrastBrightnessIntensityCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: ContrastBrightnessIntensityCommand command = new ContrastBrightnessIntensityCommand();

  3: command.Contrast = -146;

  4: command.Brightness = 358;

  5: command.Intensity = 240;

  6: command.Run(temp);

  7: afterPic.Image = temp;

13、双击radioButton7,在radioButton7 CheckedChanged事件句柄中添加以下代码:

(本段代码为AddWeightedCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: AddWeightedCommand command = new AddWeightedCommand();

  3: command.Type = AddWeightedCommandType.Average;

  4: command.Factor = new int[5];

  5: command.Factor[0] = 100;

  6: command.Factor[1] = 100;

  7: command.Factor[2] = 100;

  8: command.Factor[3] = 100;

  9: command.Run(temp);

 10: afterPic.Image = temp;

14、双击radioButton8,在radioButton8 CheckedChanged事件句柄中添加以下代码:

(本段代码为ChangeHueSaturationIntensityCommand类的使用)

  1: temp = beforePic.Image.Clone();

  2: ChangeHueSaturationIntensityCommand command = new ChangeHueSaturationIntensityCommand();

  3: ChangeHueSaturationIntensityCommandData[] data = new ChangeHueSaturationIntensityCommandData[1];

  4: data[0] = new ChangeHueSaturationIntensityCommandData();

  5: data[0].Hue = 18000;

  6: data[0].Saturation = 0;

  7: data[0].Intensity = 0;

  8: data[0].OuterLow = 315;

  9: data[0].OuterHigh = 45;

 10: data[0].InnerLow = 345;

 11: data[0].InnerHigh = 15;

 12: command.Data = data;

 13: command.Hue = 0;

 14: command.Saturation = 0;

 15: command.Intensity = 0;

 16:

 17: command.Run(temp);

 18: afterPic.Image = temp;

15、编译运行程序,本DEMO使用了10个类对图像的对比度和亮度进行调整,截图如下:

LeadTools创建“调整亮度和对比度”程序步骤

LeadTools创建“调整亮度和对比度”程序步骤

猜你喜欢