- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Xuất tất cả hình ảnh từ file Microsoft Word
Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách lấy tất cả hình ảnh trong File Document Microsoft Word bằng ngôn ngữ lập trình C#, Winform.
[C#] How to Extract all Image from Microsoft Word
Bình thường các bạn, có thể lấy hình ảnh trong file word bằng cách.
Bước 1: Đổi tên file *.docx
=> file nén *.zip
Bước 2: Giải nén file đó ra
Bước 3: Các bạn vào trong thư mục giải nén sẽ tất cả hình ảnh nằm trong thư mục media.
Tuy nhiên, đó là cách lấy hình ảnh bằng tay, nếu nhiều file word chúng ta muốn lấy hình ảnh, thì các bạn lập trình theo bài viết này.
Đầu tiên, các bạn cần cài thư viện Microsoft.Office.Interop.Word vào từ Nuget
PM> NuGet\Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1004
Video demo ứng dụng:
Full source code C#:
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace ExtractImageFromMicrosoftWord
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Title = "Open Word file";
dlg.Filter = "Document files (*.docx)|*.docx";
dlg.Multiselect = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
flowLayoutPanel1.Controls.Clear();
FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Body body = null;
MainDocumentPart mainPart = null;
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(fs, false))
{
mainPart = wdDoc.MainDocumentPart;
body = wdDoc.MainDocumentPart.Document.Body;
if (body != null)
{
var listImage = ExtractImages(body, mainPart);
const int margin = 20;
int x = margin;
int y = 4;
foreach (var item in listImage)
{
var pic = new DevExpress.XtraEditors.PictureEdit();
pic.Location = new System.Drawing.Point(x, y);
pic.Size = new Size(200, 150);
pic.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
pic.Image = item;
pic.BorderStyle = BorderStyles.Style3D;
pic.Parent = flowLayoutPanel1;
x += pic.Width;
}
}
}
fs.Flush();
fs.Close();
}
}
private List<Image> ExtractImages(Body content, MainDocumentPart wDoc)
{
var imageList = new List<Image>();
foreach (Paragraph par in content.Descendants<Paragraph>())
{
ParagraphProperties paragraphProperties = par.ParagraphProperties;
foreach (Run run in par.Descendants<Run>())
{
Drawing image = run.Descendants<Drawing>().FirstOrDefault();
if (image != null)
{
var imageFirst = image?.Inline?.Graphic?.GraphicData.Descendants<DocumentFormat.OpenXml.Drawing.Pictures.Picture>().FirstOrDefault();
if (imageFirst != null)
{
var blip = imageFirst.BlipFill.Blip.Embed.Value;
ImagePart img = (ImagePart)wDoc.Document.MainDocumentPart.GetPartById(blip);
var imgItem = Bitmap.FromStream(img.GetStream());
imageList.Add(imgItem);
}
}
}
}
return imageList;
}
}
}
Thanks for watching!