LEADTOOLS 入门教程: 将注释刻录到图像上的 C# .NET Core 控制台应用程序


 leadtools是一个综合工具包的集合,用于将识别、文档、医疗、成像和多媒体技术整合到桌面、服务器、平板电脑、网络和移动解决方案中,是一项企业级文档自动化解决方案,有捕捉,OCR,OMR,表单识别和处理,PDF,打印捕获,归档,注释和显示功能。利用业界领先的图像处理技术,能够智能识别文件,可以用来识别任何类型的扫描或传真形式的图像。

LEADTOOLS 最新下载icon-default.png?t=M85Bhttps://www.evget.com/product/782/download

本教程展示了如何使用 LEADTOOLS SDK 在 C# .NET Core 应用程序中将注释从外部 XML 文件刻录到 PDF 文档。

概括本教程介绍了将注释刻录到 C# .NET Core 控制台应用程序中的图像
完成时间30分钟
视觉工作室项目下载教程项目 (497 KB)
平台C# .NET Core 控制台应用程序
IDEVisual Studio 2019、2022
开发许可证下载 LEADTOOLS

在学习从 LEADDocument中添加和删除页面 - C# .NET Core 教程之前,通过查看添加引用和设置许可教程来熟悉创建项目的基本步骤。

创建项目并添加 LEADTOOLS 参考

从添加引用和设置许可证教程中创建的项目的副本开始。如果您没有该项目,请按照该教程中的步骤创建它。

所需的参考资料取决于项目的目的。可以通过 NuGet 包添加引用。

本教程需要以下 NuGet 包:

  • Leadtools.Document.Sdk

有关您的应用程序需要哪些 DLL 文件的完整列表,请参阅您的应用程序中包含的文件。

设置许可文件

许可证解锁项目所需的功能。它必须在调用任何工具包函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时许可证。

有两种类型的运行时许可证:

  • 评估许可证,在下载评估工具包时获得。它允许评估工具包。
  • 部署许可证。如果需要部署许可证文件和开发人员密钥,请参阅获取许可证。

添加刻录注释代码

创建项目、添加参考和许可证集后,就可以开始编码了。

在解决方案资源管理器中,打开Program.cs以下语句并将其添加到using文件顶部的块中。

【C#】

using System;
using System.IO;
using Leadtools;
using Leadtools.Annotations.Engine;
using Leadtools.Annotations.Rendering;
using Leadtools.Codecs;

在该Program.cs文件中,添加一个名为的新方法BurnAnnotationsToImage(),并在该方法之后的 Main 方法中调用它SetLicense()。源图像随项目一起提供。还有一个包含注释数据的 XML 文件。这两个文件的文件名是:

文件名描述
Burn-Annotations-to-an-Image-Source-Image.jpg图像文件
Burn-Annotations-to-an-Image-Annotations-File.xmlLEAD 注释文件

Program.cs这些文件与C# 源文件位于同一目录中。

添加以下代码以加载RasterImage,加载AnnContainer ,将AnnContainer映射到图像,将容器刻录到图像,并将新图像导出到文件。

【C#】

static void BurnAnnotationsToImage()
{

string imageFile = @"Burn-Annotations-to-an-Image-Source-Image.jpg";
string annFile = @"Burn-Annotations-to-an-Image-Annotations-File.xml";
string outputFile = @"output.jpg";

AnnDrawRenderingEngine _renderingEngine = new AnnDrawRenderingEngine();

using (RasterCodecs codecs = new RasterCodecs())
{
AnnCodecs annCodecs = new AnnCodecs();
AnnContainer container = new AnnContainer();
using (RasterImage srcImage = codecs.Load(imageFile))
{
// If you would like to use Memory Stream, then use this code:
/*
byte[] bytes = File.ReadAllBytes(imageFile);
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Position = 0;
codecs.Load(ms);
Console.WriteLine("Image loaded");
}
*/
container.Mapper.MapResolutions(srcImage.XResolution, srcImage.YResolution, srcImage.XResolution, srcImage.YResolution);
container.Size = container.Mapper.SizeToContainerCoordinates(srcImage.ImageSize.ToLeadSizeD());
container = annCodecs.Load(annFile, 1);
// Uncomment the below code to use memory stream to handle the files
// byte[] outputBytes = File.ReadAllBytes(outputFile);
// using (MemoryStream ms = new MemoryStream(outputBytes))
// {
using (RasterImage burnImage = _renderingEngine.RenderOnImage(container, srcImage))
{
codecs.Save(burnImage, outputFile, RasterImageFormat.Jpeg, 0);
string path = Path.GetFullPath(outputFile);
Console.WriteLine("Image saved" + path);
/*
ms.Position = 0;
codecs.Save(burnImage, ms, RasterImageFormat.Jpeg, 0);
string path = Path.GetFullPath(outputFile);
Console.WriteLine("Image saved" + path);
*/
}
// }
}
}
}

处理流

要使用内存流加载图像,请取消注释BurnAnnotationsToImage()方法中的代码。

【C#】

// load the image using memory stream
byte[] bytes = File.ReadAllBytes(imageFile);
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Position = 0;
codecs.Load(ms);
Console.WriteLine("Image loaded");
}

// save the stream of outputFile
byte[] outputBytes = File.ReadAllBytes(outputFile);
using (MemoryStream ms = new MemoryStream(outputBytes))
{
ms.Position = 0;
codecs.Save(burnImage, ms, RasterImageFormat.Jpeg, 0);
string path = Path.GetFullPath(outputFile);
Console.WriteLine("Image saved" + path);
}

运行项目

按F5或选择Debug -> Start Debugging运行项目。

如果正确执行了这些步骤,则应用程序会加载指定的图像,加载指定的注释 XML 文件,然后将这些注释刻录到图像并将该图像导出到文件中。以下屏幕截图显示了预期的输出:

以上便是将注释刻录到图像上的 C# .NET Core 控制台应用程序教程 ,如果您还有其他疑问,欢迎私聊我或者加入我们获取帮助!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值