- [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#] Hướng dẫn cách lấy Icon từ ứng dụng Exe trong lập trình csharp
Xin chào các bạn bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách lấy icon từ ứng dụng exe và lưu hình ảnh đó lại trong lập trình ứng dụng C#.
[C#] Extract Icon From Application Winform
Bạn có thể sử dụng bài viết này để tích hợp vào ứng dụng của mình.
VD: bạn có một list danh sách các tập tin bao gồm các file như: word, excel, image, tập tin văn bản.
Khi hiện thị danh sách thì hiển thị hình ảnh của tập tin đó kèm theo.
Dươi đây là demo ứng dụng trích xuất icon từ ứng dụng C#:
Đầu tiên các bạn tạo cho mình một class IconExtractor.cs
C#
public class IconExtractor
{
[DllImport("shell32.dll", EntryPoint = "ExtractIconEx")]
private static extern int ExtractIconExA(string lpszFile, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, int nIcons);
[DllImport("user32")]
private static extern int DestroyIcon(IntPtr hIcon);
public static Icon ExtractIconSmall(string path)
{
IntPtr largeIcon = IntPtr.Zero;
IntPtr smallIcon = IntPtr.Zero;
ExtractIconExA(path, 0, ref largeIcon, ref smallIcon, 1);
Icon returnIcon = null;
if (smallIcon != IntPtr.Zero)
returnIcon = Icon.FromHandle(smallIcon);
DestroyIcon(largeIcon);
return returnIcon;
}
public static Icon ExtractIconLarge(string path)
{
IntPtr largeIcon = IntPtr.Zero;
IntPtr smallIcon = IntPtr.Zero;
ExtractIconExA(path, 0, ref largeIcon, ref smallIcon, 1);
Icon returnIcon = null;
if (largeIcon != IntPtr.Zero)
returnIcon = Icon.FromHandle(largeIcon);
DestroyIcon(smallIcon);
return returnIcon;
}
public static Icon ExtractIcon(string path)
{
Icon largeIcon = ExtractIconLarge(path);
if (largeIcon == null)
return ExtractIconSmall(path);
else
return largeIcon;
}
}
Và tiếp đến là source code cho form main.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace IconExtractor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtPath.Text = openFileDialog1.FileName;
}
private void btnExtract_Click(object sender, EventArgs e)
{
if (txtPath.Text != string.Empty && File.Exists(txtPath.Text))
{
Icon smallIcon = IconExtractor.ExtractIconSmall(txtPath.Text);
Icon largeIcon = IconExtractor.ExtractIconLarge(txtPath.Text);
if (smallIcon != null)
pSmall.BackgroundImage = smallIcon.ToBitmap();
if (largeIcon != null)
pLarge.BackgroundImage = largeIcon.ToBitmap();
}
}
private void txtPath_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void txtPath_DragDrop(object sender, DragEventArgs e)
{
string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
if (fileNames[0].EndsWith(".exe"))
txtPath.Text = fileNames[0];
}
private void btnSaveLarge_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName.EndsWith(".bmp"))
pLarge.BackgroundImage.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
else if (saveFileDialog1.FileName.EndsWith(".png"))
pLarge.BackgroundImage.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
MessageBox.Show("Image Saved");
}
}
private void btnSaveSmall_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName.EndsWith(".bmp"))
pSmall.BackgroundImage.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
else if (saveFileDialog1.FileName.EndsWith(".png"))
pSmall.BackgroundImage.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
MessageBox.Show("Image Saved");
}
}
private void pLarge_BackgroundImageChanged(object sender, EventArgs e)
{
btnSaveLarge.Enabled = pLarge.BackgroundImage != null;
}
private void pSmall_BackgroundImageChanged(object sender, EventArgs e)
{
btnSaveSmall.Enabled = pSmall.BackgroundImage != null;
}
}
Thanks for watching!