Aspose.Words for .NET圖像處理教程——鎖定圖像的寬高比并截取圖像
Aspose.Words For .Net是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
接下來我們將進(jìn)入關(guān)于“圖像處理”的介紹,在Aspose.Words中學(xué)會(huì)如何鎖定圖像的寬高比并截取圖像。
>>Aspose.Words for .NET更新至最新版v19.12,支持轉(zhuǎn)換為PDF 1.7標(biāo)準(zhǔn),點(diǎn)擊下載體驗(yàn)
獲取點(diǎn)的實(shí)際形狀邊界
如果要在頁(yè)面上呈現(xiàn)形狀的實(shí)際邊界框,可以使用NodeRendererBase.BoundsInPoints屬性來實(shí)現(xiàn)。下面的代碼示例演示如何使用此屬性。
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);
裁剪圖像
圖像裁剪通常是指去除圖像不需要的外部部分以幫助改善取景。它還用于 去除圖像的某些 部分,以增加對(duì)特定區(qū)域的聚焦。可以使用Aspose.Words API來實(shí)現(xiàn),如下面的示例所示。
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)); }
還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。