How to get a icon from a Program running!

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
8 posts Page 1 of 1
Contributors
User avatar
Proprogrammer
VIP - Donator
VIP - Donator
Posts: 415
Joined: Sun Oct 03, 2010 11:14 pm

First you will need:
1 button
1 textbox
1 picturebox

The next 2 code boxes are for creating varibles for the button click event and what will happen when button 1 is clicked.
Code: Select all
Sub GetFileIcon(ByVal Path As String)
  Try
    If Path.ToLower.EndsWith(".jpg") Or Path.ToLower.EndsWith(".bmp") Or Path.ToLower.EndsWith(".gif") Or Path.ToLower.EndsWith(".png") Then Image.FromFile(Path)
    PictureBox1.Image = Drawing.Icon.ExtractAssociatedIcon(Path).ToBitmap()
  Catch
  End Try
Code: Select all
Public Function GetProcessPath(ByVal prc As Process) As String
  Dim query As String = String.Format("select * from Win32_Process where name='{0}.exe'", prc.ProcessName)
  Dim searcher As New ManagementObjectSearcher("root\CIMV2", query)
  Dim result = searcher.Get().GetEnumerator()
  If Not result.MoveNext() Then Throw New ArgumentException("Process not found")
  Return CStr(result.Current("ExecutablePath"))
    End Function
Just put both of the code boxes anywhere before the button1 click event.


Now for the button1 click event code.
Code: Select all
Dim proc As Process() = Process.GetProcessesByName(TextBox1.Text)
    GetFileIcon(GetProcessPath(proc(0)))
textbox1 = process you want to get icon from
picturebox1 = picture you get
button1 = starts process of getting icon from

NOTE: THIS DOES NOT SAVE THE PICTURE!
but i am sure you can figure out yourself, because that code is pretty easy.
have fun with it!
------------------------------------------------------------------------------
Proprogrammer, not just a Programmer.
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

yet another piece of code i never seen before.Thanks
Good Job Proprogrammer
You can find me on Facebook or on Skype mihai_92b
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

I'd make it so all open window icons can be displayed and saved.

Updates:
14/11/10 Fixed a possible filename bug, added better filetype save support.
21/11/10 Better memory managment for the icons
You do not have the required permissions to view the files attached to this post.
Last edited by mandai on Thu Mar 22, 2012 5:27 pm, edited 5 times in total.
User avatar
Proprogrammer
VIP - Donator
VIP - Donator
Posts: 415
Joined: Sun Oct 03, 2010 11:14 pm

Pretty cool!
------------------------------------------------------------------------------
Proprogrammer, not just a Programmer.
User avatar
Adroxxx
Member
Member
Posts: 26
Joined: Sat Aug 21, 2010 1:46 pm

I'm getting an IndexOutOfRangeException Error.
User avatar
NecroPhis
VIP - Donator
VIP - Donator
Posts: 345
Joined: Sun Aug 08, 2010 1:14 pm

nice :D thanx 4 sharing cooll;
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

ooh i just got an idea for a program :P
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

ok i just tried this and there is an error in this code
Code: Select all
 Dim searcher As New ManagementObjectSearcher("root\CIMV2", query)
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
8 posts Page 1 of 1
Return to “Tutorials”