文檔金喜正規買球>>Aspose.Words開發者指南>>Aspose.Words for .NET圖像處理教程——鎖定圖像的寬高比并截取圖像
Aspose.Words for .NET圖像處理教程——鎖定圖像的寬高比并截取圖像
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技術交流群(642018183),我們很高興為您提供查詢和咨詢。