文檔半島外圍網上直營>>Aspose中文文檔>>從Word文檔中提取圖像
                從Word文檔中提取圖像
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
使用 Aspose.Words在 Aspose.Words 中,使用Shape類來處理圖像。要選擇所有形狀節點,請使用GetChildNodes方法,并使用ImageData屬性提取圖像數據。
以下代碼示例演示如何從文檔中提取圖像并將其另存為文件:
Document doc = new Document(MyDir + "Extract image.docx");
// Save the document to memory and reload it.
using (MemoryStream stream = new MemoryStream())
{
doc.Save(stream, SaveFormat.Doc);
Document doc2 = new Document(stream);
// "Shape" nodes that have the "HasImage" flag set contain and display images.
IEnumerable<Shape> shapes = doc2.GetChildNodes(NodeType.Shape, true)
.OfType<Shape>().Where(s => s.HasImage);
int imageIndex = 0;
foreach (Shape shape in shapes)
{
string imageFileName =
$"Image.ExportImages.{imageIndex}_Aspose.Words_{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
shape.ImageData.Save(ArtifactsDir + imageFileName);
imageIndex++;
}
}
	點擊復制
使用 Open XML SDK
需要添加的命名空間:
	
using System.Collections.Generic; using System.Drawing; using System.Linq; using DocumentFormat.OpenXml.Packaging; using NUnit.Framework;
點擊復制
	
以下代碼示例演示如何從文檔中提取圖像并將其另存為文件:
public void ExtractImageFromWordDocumentFeature()
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(MyDir + "Extract image.docx", false))
{
int imgCount = doc.MainDocumentPart.GetPartsOfType<ImagePart>().Count();
if (imgCount > 0)
{
List<ImagePart> imgParts = new List<ImagePart>(doc.MainDocumentPart.ImageParts);
foreach (ImagePart imgPart in imgParts)
{
Image img = Image.FromStream(imgPart.GetStream());
string imgfileName = imgPart.Uri.OriginalString.Substring(imgPart.Uri.OriginalString.LastIndexOf("/") + 1);
img.Save(ArtifactsDir + imgfileName);
}
}
}
}
	點擊復制
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線