- [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
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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!