- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [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
[C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách chuyển đổi file ảnh XPS sang Bitmap trên C#, Winform.
[C#] How to convert XPS to Bitmap Image
Vậy tập tin XPS là gì?
File XPS hay còn gọi là XML Paper Specification (Đặc tả giấy XML mở) là dạng tài liệu mới đang được Microsoft phát triển cùng với Microsoft Silverlight, tương tự như một tệp PDF (Portable Document Format).
Các bạn viết hàm cho nút chọn hình ảnh XPS:
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "XPS Documents|*.xps" })
{
if (ofd.ShowDialog() == DialogResult.OK)
txtFileName.Text = ofd.FileName;
}
}
Tiếp đến, các bạn cần import các dll bên dưới vào project.
- WindowsBase.dll
- ReachFramework.dll
- PresentationFramework.dll
- PresentationCore.dll
Include thư viện vào project
using System;
using System.IO;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Xps.Packaging;
Đoạn code để convert file XPS to Bitmap Image C#:
public void ConvertXpsToBitmap(string fileName)
{
using (XpsDocument xpsDocument = new XpsDocument(fileName, FileAccess.Read))
{
FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
for (int pageCount = 0; pageCount < fixedDocumentSequence.DocumentPaginator.PageCount; ++pageCount)
{
DocumentPage documentPage = fixedDocumentSequence.DocumentPaginator.GetPage(pageCount);
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)documentPage.Size.Width, (int)documentPage.Size.Height, 96, 96, System.Windows.Media.PixelFormats.Default);
renderTarget.Render(documentPage.Visual);
BitmapEncoder bitmapEncoder = new BmpBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
string file = Application.StartupPath + "\\xps_page" + pageCount + ".bmp";
using (FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write))
{
bitmapEncoder.Save(fileStream);
}
}
}
}
Khi sử dụng, các bạn chỉ cần gọi.
private void btnConvert_Click(object sender, EventArgs e)
{
ConvertXpsToBitmap(txtFileName.Text);
}
Thanks for watching!