- LeadTools中文图像处理教程(1):更改数据格式 点击进入
- LeadTools中文图像处理教程(2):图像的几何变换 点击进入
- LeadTools中文图像处理教程(3):倾斜校正 点击进入
- LeadTools中文图像处理教程(4):调整图像亮度和对比度 点击进入
- LeadTools中文图像处理教程(5):图像去噪 点击进入
- LeadTools中文图像处理教程(6):检测和增强边缘、线条 点击进入
- LeadTools中文图像处理教程(7):应用艺术效果 点击进入
- LeadTools中文图像处理教程(8):调整色彩 点击进入
- LeadTools中文图像处理教程(9):添加图像至另一个图像 点击进入
- LeadTools中文图像处理教程(10):窗位(仅用于Medical) 点击进入
- LeadTools中文图像处理教程(11):对比图像 点击进入
LeadTools是全球最优秀的图形、图像处理开发包,它可以处理各种格式的文件,并包含所有图形、图像的处理和转换功能,支持多种平台。在图像处理过程中,在很多场景下我们都需要将一个图像添加至另一个图像中,即合并图像,LeadTools在合并图像方面为您提供了多种方式,如将一个图像覆盖在另一个图像的一部分上,将两个图像以固定的不透明度合并,合并图像并使一个图像看起来像另一个图像的底纹等等。
下面,赶快跟随我们的博文了解LeadTools合并图像的相关类、方法和创建程序的具体步骤吧!
本文主要包括:
1 创建“将一个图像添加至另一图像”应用程序的具体步骤
2 LeadTools“添加图像”的相关类和方法
3 简介get和put像素数据的高级/低级方法和类
创建“将一个图像添加至另一图像”应用程序的具体步骤
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)。如下图:
Name | Text |
radioButton1 | 使用CombineCommand 合并图像 |
radioButton2 | 使用CombineWarpCommand 合并图像 |
radioButton3 | 使用Underlay 合并图像 |
radioButton4 | 使用PicturizeListCommand 合并图像 |
radioButton5 | 使用PicturizeSingleCommand 合并图像 |
radioButton6 | 使用AlphaBlendCommand 合并图像 |
radioButton7 | 使用FeatherAlphaBlendCommand 合并图像 |
radioButton8 | 使用BumpMapCommand 合并图像 |
radioButton9 | 使用TextureAlphaBlendCommand 处理图像 |
radioButton10 | 使用DigitalSubtractCommand 合并图像 |
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;
8. 双击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);
9. 双击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;
10. 双击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;
11. 双击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);
12. 双击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;
13. 双击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;
14. 双击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\Image3.cmp"), 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;
15. 双击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;
16.编译运行程序,本DEMO分别使用了以上10个类进行图像处理、合并图像,结果如下图:
更多类和方法的介绍,请继续浏览下一部分!
Demo下载:
如果你希望亲自体验一下LeadTools合并图像的效果,不妨下载LeadTools试用版自己去试试。
LeadTools中将一个图像添加至目标图像的类和方法:
类名 |
说明 |
使用此类可将另一张图像的全部或部分添加至目标图像。您可以传递flag来控制是简单的使用新像素代替旧像素或是以特定的方式与旧像素合并。您可以在一个或两个图像中定义区域,这种情况下CombineCommand只影响定义的区域。此类扩展了CombineFastCommand的能力,用户可以结合具有不同每像素位数的图像。在合并图像时可以指定使用哪个颜色平面。 |
|
将原始图像的全部或部分覆盖于目标图像的全部或部分,以此种方式合并图像。 |
|
此类也可以用于合并图像,但它有一个更具体的目标。结合后一个图像看起来像另一个图像的潜在纹理。 |
|
合并列表中图片产生一个新图像,使用新图像代替一张图像。 |
|
使用一个图像的多种版本绘制一个图像。 |
|
将两个图像以一个固定的不透明度合并,创建出一个新的混合图像。 |
|
使用羽化合并两个图像,羽化时使用了取决于渐变遮罩的不透明度变量。 |
|
创建一个渐变遮罩的图像。 |
|
将图像与一个凹凸图像合并,来创建三维纹理图案。 |
|
在有底衬的效果下合并两个图形中的数据。结果可用于渐变遮罩,合并具有变量不透明度的两幅图像。还可以与使用常量不透明度的目标图像合并。 |
|
此类不合并图像,但是从遮罩图像(原始图像)减掉活动图像(目标图像),可以展现出两个图像的不同。 |
get和put像素数据的类和方法
LeadTools中有许多其他的方法允许您get和put像素数据。例如,您可以用一种颜色填充一个图像,然后get和put单独的像素数据,这些类和方法分为高级和低级两类,如下:
高级类和方法:
类 |
说明 |
将指定图像的所有位设为0,将其转变为黑色图像。 |
|
将所有颜色值为负的像素设为0 |
|
通过调整色相、饱和度和亮度的值替换指定的颜色。 |
|
使用八个色彩空间中的任意一个重置这些图像的像素值,这个值会落在指定区域的内部或外部。 |
|
将有签名的图像转换为无签名的图像。 |
|
以指定颜色填充指定的图像。 |
|
以指定的颜色代替像素中的红色分量。在图像中只替换眼睛区域的像素,因此需在眼部周围选择一个小的区域。 |
低级方法:
方法 |
说明 |
返回指定像素的颜色。 |
|
将指定像素的像素数据拷贝到一个未托管的内存缓冲区中。 |
|
接受一个列偏移量,从图像中检索数据并将其放入缓冲区。 |
|
GetRow方法 |
从RasterImage中检索一行或多行图像数据,并经其放入托管的内存缓冲区中。 |
分配三个数组,并以特定行中每个像素的R、G和B的配置文件更新数组。 |
|
改变指定像素的颜色值。 |
|
通过从非托管内存缓冲区中复制,来改变指定像素的数据。 |
|
SetRow方法 |
将图像数据的一行或多行复制到RasterImage |
使用图像偏移量将数据从缓冲区复制到一个图像。 |
将图像看做设备上下文会提供更多的可能性。使用CreateLeadDC方法可以获取设备上下文。您可以使用Windows GDI方法在图像上绘制线、文本或图像。此外,您还可以使 用 Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer方法创建一个用于绘制的GDI+图形对象。
若您在使用LeadTools的过程中遇到任何问题,欢迎在葡萄城开发者社区LeadTools板块提问,我们的专业技术团队期待您的到来!