Ir para conteúdo
Faça parte da equipe! (2024) ×

C#] Criando um Extractor de Ícones


Programador_Hack
 Compartilhar

Posts Recomendados

Primeiramente, adicione este namespace

 

Quote

using System.Runtime.InteropServices;

 

 

~> Adicione 2 Buttons e 2 PictureBox (Um para o Icone menor e outro para o Icone Maior)

 

~> Adicione também 1 openfiledialog e 1 savefiledialog

 

Organize mais ou menos dessa forma:

 

Sem_t_tulo_8.jpg

 

 

No text do Button1, coloque Extrair Icon

 

No text do Button2, coloque Salvar Icone

 

Renomeie o PictureBox1 para picIconSmall

 

Renomeie o PictureBox2 para picIconLarge

 

Declare isto (Acima de Public Form1() ... )

 

 

 

Citaçao

 

""// Constants that we need in the function call

private const int SHGFI_ICON = 0x100;

private const int SHGFI_SMALLICON = 0x1;

private const int SHGFI_LARGEICON = 0x0;

 

// This structure will contain information about the file

public struct SHFILEINFO

{

// Handle to the icon representing the file

public IntPtr hIcon;

// Index of the icon within the image list

public int iIcon;

// Various attributes of the file

public uint dwAttributes;

// Path to the file

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]

public string szDisplayName;

// File type

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

public string szTypeName;

};

 

// The signature of SHGetFileInfo (located in Shell32.dll)

[DllImport("Shell32.dll")]

public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);

 

 

 

No Button de extrair, adicione este code

 

 

 

 

 

Citação:

// Will store a handle to the small icon

IntPtr hImgSmall;

// Will store a handle to the large icon

IntPtr hImgLarge;

 

SHFILEINFO shinfo = new SHFILEINFO();

 

// Open the file that we wish to extract the icon from

if(openFile.ShowDialog() == DialogResult.OK)

{

// Store the file name

string FileName = openFile.FileName;

// Sore the icon in this myIcon object

System.Drawing.Icon myIcon;

 

// Get a handle to the small icon

hImgSmall = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);

// Get the small icon from the handle

myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

// Display the small icon

picIconSmall.Image = myIcon.ToBitmap();

 

// Get a handle to the large icon

hImgLarge = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);

// Get the large icon from the handle

myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

// Display the large icon

picIconLarge.Image = myIcon.ToBitmap();

}

 

No button Salvar adicone este :

 

 

 

Citação:

saveFileDialog1.FileName = "Icone";

saveFileDialog1.Title = "Salvar Icone";

saveFileDialog1.Filter = "Icones|*Icon";

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

Image Imagem = picIconLarge.Image;

Imagem.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Icon);

}

 

 

 

 

Veja o Resultado

 

 

 

Creditos

Programador_Hack

 

Agradeçao

Link para o comentário
Compartilhar em outros sites

Este tópico está impedido de receber novos posts.
 Compartilhar

  • Quem Está Navegando   0 membros estão online

    • Nenhum usuário registrado visualizando esta página.
×
×
  • Criar Novo...

Informação Importante

Nós fazemos uso de cookies no seu dispositivo para ajudar a tornar este site melhor. Você pode ajustar suas configurações de cookies , caso contrário, vamos supor que você está bem para continuar.