LeadTools创建“将一图像添加至另一图像”步骤

2026-04-01 16:46:32

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

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.Codecs.Bmp.dll

Leadtools.ImageProcessing.Core.dll      

Leadtools.ImageProcessing.Effects.dll      

Leadtools.ImageProcessing.SpecialEffects.dll      

Leadtools.WinForms.dll 

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

LeadTools创建“将一图像添加至另一图像”步骤

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

  1: using Leadtools;

  2: using Leadtools.WinForms;

  3: using Leadtools.Codecs;

  4: using Leadtools.Codecs.Cmp;

  5: using Leadtools.Codecs.Bmp;

  6: using Leadtools.ImageProcessing;

  7: using Leadtools.ImageProcessing.Core;

  8: using Leadtools.ImageProcessing.Effects;

  9: using Leadtools.ImageProcessing.SpecialEffects;

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\cannon.jpg"));

7、双击radioButton1,在radioButton1 CheckedChanged事件句柄中添加以下代码:(本段代码为  CombineCommand类的使用)

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

  2:

  3: CombineCommand command = new CombineCommand();

  4: command.SourceImage = temp.Clone();

  5: command.DestinationRectangle = new LeadRect(temp.Width / 8, temp.Height / 8, temp.Width, temp.Height);

  6: command.SourcePoint = new LeadPoint(0, 0);

  7: command.Flags = CombineCommandFlags.OperationAdd | CombineCommandFlags.Destination0 | CombineCommandFlags.SourceRed | CombineCommandFlags.DestinationGreen | CombineCommandFlags.ResultBlue;

  8: command.Run(temp);

  9: afterPic.Image = temp;

8、双击radioButton2,在radioButton2 CheckedChanged事件句柄中添加以下代码(本段代码为 CombineWarpCommand类的使用):

  1: RasterImage parentImage = beforePic.Image.Clone();

  2: RasterImage childImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"));

  3: // 合并图像

  4: CombineWarpCommand command = new CombineWarpCommand();

  5:

  6: LeadPoint[] destPoints =

  7:    {

  8:       new LeadPoint(100,100),

  9:       new LeadPoint(200,75),

 10:       new LeadPoint(200,200),

 11:       new LeadPoint(100,200)

 12:    };

 13:

 14: command.DestinationImage = parentImage;

 15: command.SetDestinationPoints(destPoints);

 16: command.SourceRectangle = new LeadRect(0, 0, childImage.Width, childImage.Height);

 17: command.Flags = CombineWarpCommandFlags.Bilinear;

 18: command.Run(childImage);

 19: // 保存至磁盘

 20: codecs.Save(parentImage, Path.Combine(Application.StartupPath, @"..\..\Pic\CombineWarpCommand.bmp"), RasterImageFormat.Bmp, 24);

 21: afterPic.Image = parentImage;

9、双击radioButton3,在radioButton3 CheckedChanged事件句柄中添加以下代码(本段代码为Underlay类的使用):

  1: RasterImage image = beforePic.Image.Clone();

  2: // 加载底层的图像

  3: RasterImage underlayImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"));

  4: // 使用underlayImage作为图像平铺的衬底

  5: image.Underlay(underlayImage, RasterImageUnderlayFlags.None);

  6: afterPic.Image = image;

  7: codecs.Save(image, Path.Combine(Application.StartupPath, @"..\..\Pic\UnderLay.jpg"), RasterImageFormat.Bmp, 0);

10、双击radioButton4,在radioButton4 CheckedChanged事件句柄中添加以下代码(本段代码为 PicturizeListCommand类的使用):

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

  2: PicturizeListCommand command = new PicturizeListCommand();

  3: command.CellHeight = 40;

  4: command.CellWidth = 30;

  5: command.LightnessFactor = 200;

  6: command.Run(temp);

  7: afterPic.Image = temp;

11、双击radioButton5,在radioButton5 CheckedChanged事件句柄中添加以下代码(本段代码为  PicturizeSingleCommand类的使用):

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

  2: //准备命令

  3: PicturizeSingleCommand command = new PicturizeSingleCommand();

  4: command.CellWidth = 40;

  5: command.CellHeight = 30;

  6: command.LightnessFactor = 200;

  7: command.ThumbImage = temp;

  8: //绘制位图

  9: command.Run(temp);

 10: afterPic.Image = temp;

12、双击radioButton6,在radioButton6 CheckedChanged事件句柄中添加以下代码(本段代码为 AlphaBlendCommand类的使用):

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

  2: // 准备命令

  3: RasterImage SrcImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"), 4, CodecsLoadByteOrder.Bgr, 1, 1);

  4: AlphaBlendCommand command = new AlphaBlendCommand();

  5: //使用一半的不透明度合并SrcImage和temp

  6: command.DestinationRectangle = new LeadRect(temp.Width / 8, temp.Height / 8, temp.Width, temp.Height);

  7: command.SourceImage = SrcImage;

  8: command.Opacity = 128;

  9: command.Run(temp);

13、双击radioButton7,在radioButton7 CheckedChanged事件句柄中添加以下代码(本段代码为  FeatherAlphaBlendCommand类的使用):

  1: RasterImage backgroundImage = beforePic.Image.Clone();

  2: RasterImage sourceImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"), 32, CodecsLoadByteOrder.Bgr, 1, 1);

  3: FeatherAlphaBlendCommand command = new FeatherAlphaBlendCommand();

  4: command.DestinationRectangle = new LeadRect(0, 0, sourceImage.Width/2, sourceImage.Height/2);

  5: command.MaskImage = sourceImage.CreateAlphaImage();

  6: command.SourceImage = sourceImage;

  7: command.SourcePoint = new LeadPoint(0, 0);

  8: command.Run(backgroundImage);

  9: afterPic.Image = backgroundImage;

14、双击radioButton8,在radioButton8 CheckedChanged事件句柄中添加以下代码(本段代码为 BumpMapCommand类的使用):

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

  2: // 准备命令

  3: BumpMapCommand command = new BumpMapCommand();

  4: command.Azimuth = 5;

  5: command.Brightness = 50;

  6: command.BumpImage = temp;

  7: command.BumpPoint = new LeadPoint(0, 0);

  8: command.Depth = 3;

  9: command.DestinationPoint = new LeadPoint(0, 0);

 10: command.Elevation = 0;

 11: command.Intensity = 0;

 12: command.LookupTable = null;

 13: command.Tile = true;

 14: command.Run(temp);

 15: afterPic.Image = temp;

15、双击radioButton9,在radioButton9 CheckedChanged事件句柄中添加以下代码(本段代码为 TextureAlphaBlendCommand类的使用):

  1: // 准备命令

  2: RasterImage image;

  3: RasterImage SrcImage;

  4: RasterImage MaskImage;

  5: RasterImage underlayImage;

  6:

  7: image = beforePic.Image.Clone();

  8: SrcImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image75pxp"), 4, CodecsLoadByteOrder.Bgr, 1, 1);

  9: //加载遮罩图像

 10: MaskImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\FadeMask.bmp"), 4, CodecsLoadByteOrder.Bgr, 1, 1);

 11: underlayImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"), 4, CodecsLoadByteOrder.Bgr, 1, 1);

 12:

 13: TextureAlphaBlendCommand command = new TextureAlphaBlendCommand();

 14: command.DestinationRectangle = new LeadRect(image.Width / 2 - MaskImage.Width / 2, image.Height / 2 - MaskImage.Height / 2, MaskImage.Width, MaskImage.Height);

 15: command.MaskImage = MaskImage;

 16: command.Opacity = 100;

 17: command.SourceImage = SrcImage;

 18: command.SourcePoint = new LeadPoint(0, 0);

 19: command.UnderlayImage = underlayImage;

 20: command.Run(image);

 21: afterPic.Image = image;

16、双击radioButton10,在radioButton10 CheckedChanged事件句柄中添加以下代码(本段代码为 DigitalSubtractCommand类的使用):

  1: RasterImage image = beforePic.Image.Clone();

  2: // 准备命令

  3: RasterImage MaskImage;

  4: MaskImage = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Image1.jpg"), 4, CodecsLoadByteOrder.Bgr, 1, 1);

  5:

  6: DigitalSubtractCommand command = new DigitalSubtractCommand();

  7: command.Flags = DigitalSubtractCommandFlags.ContrastEnhancement;

  8: command.MaskImage = MaskImage;

  9: command.Run(image);

 10: afterPic.Image = image;

17、编译运行程序,本DEMO分别使用了以上10个类进行图像处理、合并图像,结果如下图:

LeadTools创建“将一图像添加至另一图像”步骤

LeadTools创建“将一图像添加至另一图像”步骤

猜你喜欢