NEWS

[C#] Hướng dẫn cách lấy Icon từ ứng dụng Exe trong lập trình csharp

[C#] Hướng dẫn cách lấy Icon từ ứng dụng Exe trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 4351 09:47:37, 11/06/2019C#   In bài viết

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#:

extract_icon_csharp_demo

 

Đầ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!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn cách lấy Icon từ ứng dụng Exe trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 4351 09:47:37, 11/06/2019C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.