Aspose.Words for .NET图像处理教程——锁定图像的宽高比并截取图像

本文我们将进入关于“图像处理”的介绍,在Aspose.Words中学会如何锁定图像的宽高比并截取图像。

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入关于“图像处理”的介绍,在Aspose.Words中学会如何锁定图像的宽高比并截取图像。

>>Aspose.Words for .NET更新至最新版v19.12,支持转换为PDF 1.7标准,体验


获取点的实际形状边界

如果要在页面上呈现形状的实际边界框,可以使用NodeRendererBase.BoundsInPoints属性来实现。下面的代码示例演示如何使用此属性。

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);var shape = builder.InsertImage(dataDir + "Test.png");shape.AspectRatioLocked = false;dataDir = dataDir + "Shape_AspectRatioLocked_out.doc";// Save the document to disk.doc.Save(dataDir);

裁剪图像

图像裁剪通常是指去除图像不需要的外部部分以帮助改善取景。它还用于 去除图像的某些 部分,以增加对特定区域的聚焦。可以使用Aspose.Words API来实现,如下面的示例所示。

string dataDir = RunExamples.GetDataDir_WorkingWithImages();string inputPath = dataDir + "ch63_Fig0013.jpg";string outputPath = dataDir + "cropped-1.jpg";CropImage(inputPath,outputPath, 124, 90, 570, 571);
public static void CropImage(string inPath, string outPath, int left, int top,int width, int height){    Document doc = new Document();    DocumentBuilder builder = new DocumentBuilder(doc);    Image img = Image.FromFile(inPath);    int effectiveWidth = img.Width - width;    int effectiveHeight = img.Height - height;    Shape croppedImage = builder.InsertImage(img,        ConvertUtil.PixelToPoint(img.Width - effectiveWidth),        ConvertUtil.PixelToPoint(img.Height - effectiveHeight));    double widthRatio = croppedImage.Width / ConvertUtil.PixelToPoint(img.Width);    double heightRatio = croppedImage.Height / ConvertUtil.PixelToPoint(img.Height);    if (widthRatio< 1) croppedImage.ImageData.CropRight = 1 - widthRatio; if (heightRatio< 1) croppedImage.ImageData.CropBottom = 1 - heightRatio; float leftToWidth = (float)left / img.Width; float topToHeight = (float)top / img.Height; croppedImage.ImageData.CropLeft = leftToWidth; croppedImage.ImageData.CropRight = croppedImage.ImageData.CropRight - leftToWidth; croppedImage.ImageData.CropTop = topToHeight; croppedImage.ImageData.CropBottom = croppedImage.ImageData.CropBottom - topToHeight; croppedImage.GetShapeRenderer().Save(outPath, new ImageSaveOptions(SaveFormat.Jpeg)); }

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年12月26日
下一篇 2020年1月1日

相关推荐

发表回复

登录后才能评论