翻譯|使用教程|編輯:李顯亮|2020-01-02 09:34:32.477|閱讀 472 次
概述:本文我們將進入關于“圖像處理”的介紹,在Aspose.Words中學會如何鎖定圖像的寬高比并截取圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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)); }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn