LeadTools创建“加速图像分辨率转换 ”程序步骤

2026-02-26 12:38:12

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

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.ImageProcessing.Core.dll

Leadtools.WinForms.dll 

3、从工具箱(视图->工具箱),添加2个Button控件(将Button的Text属性分别改为“加载图像”和“使用RasterUserMatchTable在改变图像分辨率时加快转换速度”)。

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

  1: using Leadtools;

  2: using Leadtools.Codecs;

  3: using Leadtools.ImageProcessing;

  4: using Leadtools.ImageProcessing.Core;

  5: using Leadtools.WinForms;

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

  1: private RasterImage image;

  2: private RasterImageViewer imageViewer;

  3: private RasterCodecs codecs;

6、双击button1,在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、双击button2,在button2 Click事件句柄中添加以下代码:

  1: if (image == null)

  2: {

  3:     MessageBox.Show("请首先加载图像!");

  4:     return;

  5: }

  6: RasterImage destImage = image.Clone();

  7: // 64色彩虹调色板

  8: RasterColor[] colors =

  9:        {

 10:           new RasterColor(0, 0, 0), new RasterColor(0, 0, 85), new RasterColor(0, 0, 170), new RasterColor(0, 0, 255),

 11:           new RasterColor(85, 0, 0), new RasterColor(85, 0, 85), new RasterColor(85, 0, 170), new RasterColor(85, 0, 255),

 12:           new RasterColor(170, 0, 0), new RasterColor(170, 0, 85), new RasterColor(170, 0, 170), new RasterColor(170, 0, 255),

 13:           new RasterColor(255, 0, 0), new RasterColor(255, 0, 85), new RasterColor(255, 0, 170), new RasterColor(255, 0, 255),

 14:           new RasterColor(0, 85, 0), new RasterColor(0, 85, 85), new RasterColor(0, 85, 170), new RasterColor(0, 85, 255),

 15:           new RasterColor(85, 85, 0), new RasterColor(85, 85, 85), new RasterColor(85, 85, 170), new RasterColor(85, 85, 255),

 16:           new RasterColor(170, 85, 0), new RasterColor(170, 85, 85), new RasterColor(170, 85, 170), new RasterColor(170, 85, 255),

 17:           new RasterColor(255, 85, 0), new RasterColor(255, 85, 85), new RasterColor(255, 85, 170), new RasterColor(255, 85, 255),

 18:           new RasterColor(0, 170, 0), new RasterColor(0, 170, 85), new RasterColor(0, 170, 170), new RasterColor(0, 170, 255),

 19:           new RasterColor(85, 170, 0), new RasterColor(85, 170, 85), new RasterColor(85, 170, 170), new RasterColor(85, 170, 255),

 20:           new RasterColor(170, 170, 0), new RasterColor(170, 170, 85), new RasterColor(170, 170, 170), new RasterColor(170, 170, 255),

 21:           new RasterColor(255, 170, 0), new RasterColor(255, 170, 85), new RasterColor(255, 170, 170), new RasterColor(255, 170, 255),

 22:           new RasterColor(0, 255, 0), new RasterColor(0, 255, 85), new RasterColor(0, 255, 170), new RasterColor(0, 255, 255),

 23:           new RasterColor(85, 255, 0), new RasterColor(85, 255, 85), new RasterColor(85, 255, 170), new RasterColor(85, 255, 255),

 24:           new RasterColor(170, 255, 0), new RasterColor(170, 255, 85), new RasterColor(170, 255, 170), new RasterColor(170, 255, 255),

 25:           new RasterColor(255, 255, 0), new RasterColor(255, 255, 85), new RasterColor(255, 255, 170), new RasterColor(255, 255, 255)

 26:        };

 27:

 28: // 创建和设置user match table

 29: RasterUserMatchTable userMatchTable = new RasterUserMatchTable();

 30: userMatchTable.Create(colors);

 31: userMatchTable.Use();

 32:

 33: // 使用新调色板更改颜色分辨率。注意若您多次使用,user match table 使您的代码速度更快。它的

 34: //代码在这里出现是因为我们想要向您展示它是怎么使用的。

 35:

 36: ColorResolutionCommand command = new ColorResolutionCommand(

 37:    ColorResolutionCommandMode.InPlace,

 38:    8,

 39:    RasterByteOrder.Rgb,

 40:    RasterDitheringMethod.FloydStein,

 41:    ColorResolutionCommandPaletteFlags.UsePalette | ColorResolutionCommandPaletteFlags.FastMatch,

 42:    colors);

 43: command.Run(destImage);

 44:

 45: //若您不再需要使用user match table,释放它

 46: userMatchTable.Unuse();

 47:

 48: imageViewer.Image = destImage;

 49: // 将图像保存回磁盘

 50: codecs.Save(destImage, Path.Combine(Application.StartupPath, @"..\..\Pic\result.bmp"), RasterImageFormat.Bmp, 8);

8、编译运行程序,本示例使用RasterUserMatchTable加速图像分辨率转换的速度。结果如下图所示:

LeadTools创建“加速图像分辨率转换 ”程序步骤

LeadTools创建“加速图像分辨率转换 ”程序步骤

猜你喜欢