LeadTools创建“改变图像颜色分辨率”程序步骤
1、打开Visual Studio .NET。点击 文件->新建->项目…。打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“SampleColorResolutionCommand”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
2、在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32”,选择以下的DLL“:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Bmp.dll
Leadtools.Codecs.Cmp.dll
Leadtools.WinForms.dll
3、从工具箱(视图->工具箱),添加2个Button控件(将Button的Text属性分别改为“加载图像”和“将图像分辨率转为每像素8位”)。
4、切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:
1: using Leadtools;
2: using Leadtools.Codecs;
3: using Leadtools.Codecs.Cmp;
4: using Leadtools.ImageProcessing;
5: using Leadtools.WinForms;
5、将以下变量添加至Form1类:
1: private RasterImage image;
2: private RasterImageViewer imageViewer;
3: private RasterCodecs codecs;
6、双击“加载图像”按钮,在button1 Click事件句柄中添加以下代码:
1: // 加载图像
2: codecs = new RasterCodecs();
3:
4: codecs.ThrowExceptionsOnInvalidImages = true;
5: image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\IMAGE25pxp"));
6:
7: imageViewer = new RasterImageViewer();
8: imageViewer.Image = image;
9: imageViewer.Height = 1200;
10: imageViewer.Width = 1000;
11: imageViewer.Location = new System.Drawing.Point(0, 50);
12: Controls.Add(imageViewer);
13: imageViewer.BringToFront();
7、双击“将图像分辨率转为每像素8位”按钮,在button2 Click事件句柄中添加以下代码:
1: if (image == null)
2: {
3: MessageBox.Show("请首先加载图像!");
4: return;
5: }
6: // 将颜色分辨率转为每像素8位,在单独的图像中使用Netscape调色板
7: ColorResolutionCommand cmd = new ColorResolutionCommand();
8: cmd.Mode = ColorResolutionCommandMode.CreateDestinationImage;
9: cmd.BitsPerPixel = 8;
10: cmd.Order = RasterByteOrder.Rgb;
11: cmd.DitheringMethod = RasterDitheringMethod.None;
12: cmd.PaletteFlags = ColorResolutionCommandPaletteFlags.UsePalette;
13: cmd.SetPalette(RasterPalette.Netscape());
14: cmd.Run(image);
15:
16: RasterImage destImage = cmd.DestinationImage;
17: System.Diagnostics.Contracts.Contract.Assert(destImage.BitsPerPixel == 8);
18:
19: imageViewer.Image = destImage;
20: // 保存至磁盘
21: codecs.Save(destImage, Path.Combine(Application.StartupPath, @"..\..\Pic\Image1_colorres8.bmp"), RasterImageFormat.Bmp, 8);
8、编译运行程序,本示例使用ColorResolutionCommand类改变图像分辨率,截图如下:

