- Tạo bản quyền phần mềm C# và bảo mật code | The Enigma Protector
- [C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform
- [C#] Lập trình ứng dụng lấy ngày giờ hệ thống mạng LAN sử dụng giao thức UDP
- [DATABASE] Sự khác nhau giữa hai câu lệnh TRUNCATE vs DELETE trong sqlserver
- [C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
[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!