- [VB.NET] Lấy địa chỉ Bios Serial Number trên Winform
- [C#] Giới thiệu và sử dụng thư viện AutoMapper
- [DEVEXPRESS] Hướng dẫn Custom Summary in Footer trong Gridview C#
- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
[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!