LeadTools创建“倾斜校正”应用程序的具体步骤

2026-02-28 06:26:09

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

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

Leadtools.dll

Leadtools.Codecs.dll

Leadtools.Codecs.Tif.dll

Leadtools.ImageProcessing.Core.dll      

Leadtools.WinForms.dll 

3、从工具箱(视图->工具箱),添加2个Button控件(将Text属性分别改为加载图像和倾斜校正)。

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

  1: using Leadtools;

  2: using Leadtools.Codecs;

  3: using Leadtools.Codecs.Tif;

  4: using Leadtools.ImageProcessing.Core;

  5: using Leadtools.WinForms;

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

  1: private RasterImage image;

  2: private RasterImageViewer imageViewer;

6、双击“加载图像”按钮,在button1 Click事件句柄中添加以下代码:

  1: // 加载图像

  2: codecs = new RasterCodecs();

  3:

  4: codecs.ThrowExceptionsOnInvalidImages = true;

  5: image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\clean.tif"));

  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 Click事件句柄中添加以下代码:

  1:  if (image == null)

  2:  {

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

  4:      return;

  5:  }

  6:  // 准备命令

  7:  Leadtools.ImageProcessing.Core.DeskewCommand command = new Leadtools.ImageProcessing.Core.DeskewCommand();

  8:  //图像的倾斜校正

  9:  command.Flags = DeskewCommandFlags.DeskewImage | DeskewCommandFlags.DoNotFillExposedArea;

 10:  command.FillColor = new Leadtools.RasterColor(255, 255, 255);

 11:  command.Run(image);

 12:  codecs.Save(image, Path.Combine(Application.StartupPath, @"..\..\Pic\cleanResult.tif"), RasterImageFormat.Tif, 24);

8、编译运行程序,结果如下图所示:

原图:

LeadTools创建“倾斜校正”应用程序的具体步骤

9、校正后的图像:

LeadTools创建“倾斜校正”应用程序的具体步骤

猜你喜欢