0

I'm makeing a program that mimics the desktop, i've got it all working but i'm only getting the lowest Resolution out from the icons.

I need to get all of the icons from files in a set path, these icons need to be the best they can (256x256).
I want to get Icon from files like .exe .lnk .url .txt .png . jpg
this is a Shortend Verstion what i have currently.(I remove all the part that don't have any affect on the icons)

Public Class Form1
Dim counter As Integer = 0
Dim IconArray(200) As PictureBox
Dim TitleArray(200) As Label
Dim linker(200) As String
Public Sub loading(sender As Object, e As EventArgs) Handles MyBase.Load
    Apps_on_Screen.Items.AddRange(IO.Directory.GetFiles("C:\Users\Public\Desktop")) 'Gets All files on Desktop and adds them to listbox
    Threading.Thread.Sleep(0) 'waits till all file have been got from desktop
    Dim Icon_Position_X = 0, Icon_Position_Y = 5 'Sets the start X and Y for the Icons
    Dim Title_Position_X = 0, Title_Position_Y = 55 'Sets the start X and Y for the Text under the Icons
    For i As Integer = 0 To Apps_on_Screen.Items.Count - 1 'Loops for every item in listbox 
        Dim Icon As New PictureBox 'Creates new Picturebox
        Icon.BackgroundImageLayout = ImageLayout.Zoom 'Changes it to zoom so the images fits
        Icon.BackColor = Color.Transparent 'Makes Picturebox Transparent
        Icon.Name = "Icon" & i 'Gives picturebox a name for a referance later
        Icon.Size = New Size(78, 51) 'Sets the Picturebox a size
        Icon.Location = New Point(Icon_Position_X, Icon_Position_Y) 'Sets the Pictureboxs X and Y
        Me.Controls.Add(Icon) 'Adds picturebox to controls
        IconArray(i) = Icon 'Adds picturebox to an Array for easyer use later
        IconArray(i).BringToFront()
        AddHandler Icon.DoubleClick, AddressOf Me.Start_Link 'Adds a Double click Handler for picturebox on A Sub
        Icon_Position_Y += 101 'Changes the Y Postion on next Picturebox
        If (Icon_Position_Y + 50) >= Screen.PrimaryScreen.WorkingArea.Height Then 'When the Picture box reaches the bottom of the sceen the Y Resets and The X Gets Increased
            Icon_Position_Y = 5
            Icon_Position_X += 78
        End If
        'Same as Above, But with a Label
        Dim Title As New Label
        Title.Name = "Title" & i
        Title.Size = New Size(78, 51)
        Title.Location = New Point(Title_Position_X, Title_Position_Y)
        Title.ForeColor = Color.White
        Title.BackColor = Color.Transparent
        Title.TextAlign = ContentAlignment.TopCenter
        Title.Font = New Font("Microsoft Sans Serif", 9)
        Me.Controls.Add(Title)
        TitleArray(i) = Title
        Title.BringToFront()
        AddHandler Title.DoubleClick, AddressOf Me.Start_Link
        Title_Position_Y += 101
        If Title_Position_Y >= Screen.PrimaryScreen.WorkingArea.Height Then
            Title_Position_Y = 55
            Title_Position_X += 78
        End If
    Next
    Set_icons()
End Sub
Public Sub Set_icons()
    'Most PC's have "This PC" on the Desktop
    IconArray(0).BackgroundImage = Fake_Background.My.Resources.This_PC
    TitleArray(0).Text = "This PC"
    linker(0) = "C:\WINDOWS\explorer.exe"
    counter = 1
    For Each Item In Apps_on_Screen.Items 'loops for every item in listbox
        Try 'Just here incase it crashes
            linker(counter) = Item 'adds item to array to be use to start the programs
            IconArray(counter).BackgroundImage = Icon.ExtractAssociatedIcon(Item).ToBitmap 'Gets a 32x32 icon from item then sets it as the icons background
            Dim Extension = IO.Path.GetExtension(Item) 'Gets the extension from item, to be used later
            Item = IO.Path.GetFileName(Item) 'Gets the Items name E.G Item = "C:\Users\Public\Desktop\Hi.txt" now item = "Hi.txt"
            If Item = "desktop.ini" Then '"desktop.ini" Didn't want to work thats why i have the code at the top of this sub
                counter -= 1
            Else
                Item = Item.Replace(Extension, "") 'Removes the extension from item E.G item = "Hi.txt" now item = "Hi"
                TitleArray(counter).Text = Item 'Sets the display text for the item
                'This part is how i Get the higher Res icons
                Item = Item.Replace(" ", "-") 'Replaces " " with "-"
                'I've got a bunch of images of icons in Resources
                IconArray(counter).BackgroundImage = DirectCast(My.Resources.ResourceManager.GetObject(Item), Bitmap) 'Seaches for Item in Resources if its found its add set to the icons background
            End If
        Catch ex As Exception
        End Try
        counter += 1
    Next
End Sub
Public Sub Start_Link(sender As Object, e As EventArgs)
    'i don't know how to explane this
    Dim start = 0
    If sender.Name.Contains("Icon") Then start = Val(Replace(sender.Name, "Icon", ""))
    If sender.Name.Contains("Title") Then start = Val(Replace(sender.Name, "Title", ""))
    Try
        Process.Start(linker(start))
    Catch ex As Exception
        If linker(start).Contains(" (x86)") Then 'some programs didn't want to work so i added this as a fix
            linker(start) = Replace(linker(start), " (x86)", "")
            Process.Start(linker(start))
        End If
    End Try
End Sub
End Class

This code only grabs the 32X32 size Images of icon to compair Desktop, In Program

Is there a way for me to get the best Resolution icon.
Can you post the code? as i've spent an hour just looking around following links, but it's all in c# and not to much help for me.

Thanks ~Gamer_sa22

Gamer_sa22
  • 15
  • 5
  • Possible duplicate of [Trying to load an application's icon(s) using LoadImage, but the function returns 0](https://stackoverflow.com/questions/32606715/trying-to-load-an-applications-icons-using-loadimage-but-the-function-return) -- Check my answer to that question. – Visual Vincent Jul 28 '17 at 10:38
  • Is there a particular reason that you're using `ExtractAssociatedIcon` instead of one of the constructors for `Icon`? There are two overloads that will construct from a string with a specified size, either as `Size` or as width and height. If you ask for e.g. 256x256, by my reading of the MSDN docs, you should get the nearest available size. – Craig Jul 28 '17 at 14:05
  • Visual Vincent i tryed that but i keep getting "'System.Runtime.InteropServices.NativeMethods' is not accessible in this context is friend" do you know the fix – Gamer_sa22 Jul 31 '17 at 00:37
  • Craig i'm useing ExtractAssociatedIcon while i find a way to get the 256x256, also i thought you need it to be in resouses to use ICON – Gamer_sa22 Jul 31 '17 at 00:39
  • Visual Vincent, i got the nativemethods works but i'm still only getting low res icons, also it cant do .lnk – Gamer_sa22 Jul 31 '17 at 02:55
  • @Gamer_sa22 : Could you update the question with the code you're using (only the part that isn't already in my answer)? Also, type "@VisualVincent" to notify me about comments. :) – Visual Vincent Jul 31 '17 at 10:20
  • Getting the icon from a shortcut is quite different. The location to the icon of the shortcut is stored within the shortcut itself. See this for a C# solution: https://stackoverflow.com/questions/343200/extract-icon-from-windows-lnk-shortcut-file – Visual Vincent Jul 31 '17 at 10:31
  • @VisualVincent ok i'll update it, do you want me to post all of my code or just the parts that matter? it's going to take me about 2 days to update as i don't have access to the computer it's on right now. Sorry if the codes Messy i've just been chucking code at it trying to get it to work. – Gamer_sa22 Jul 31 '17 at 11:30
  • By my reading of the documentation, the `Icon(String, Size)` ctor will initialize from a file, not from a resource. There is a separator ctor overload for initializing from a resource. – Craig Jul 31 '17 at 12:14
  • That code you added is not related to the code in my answer... – Visual Vincent Jul 31 '17 at 18:47
  • @VisualVincent I've added my code, and tryed to comment what i was doing – Gamer_sa22 Aug 03 '17 at 00:16
  • That is still not related to the answer that I linked you to. If you say that _**my code**_ doesn't give you the largest icon then you have to show me how you use it. – Visual Vincent Aug 03 '17 at 06:59
  • @VisualVincent i've inserted the code below, i also have all the other stuff too. `For Each Item In Apps_on_Screen.Items Try linker(counter) = Item Dim Icons As AssemblyIconCollection = ExtractAssociatedIcons(Item) For Each SmallIcon As Icon In Icons.SmallIcons IconArray(counter).BackgroundImage = SmallIcon.ToBitmap Next For Each LargeIcon As Icon In Icons.LargeIcons IconArray(counter).BackgroundImage = LargeIcon.ToBitmap Next Dim Extension = IO.Path.GetExtension(Item)` do you want me to add the code to the question or will this do? – Gamer_sa22 Aug 03 '17 at 12:49
  • You are iterating through _every_ existing icon in the file and setting it to be the background image. Since you _**set**_ the image you replace the current image for every iteration, thus you'll only get the last large icon it finds. – Visual Vincent Aug 03 '17 at 16:45
  • In your case you only need the `LargeIcon` loop. Declare a temporary `Icon` variable outside the loop, then inside the loop you check the size of the icon in this variable (if it isn't null) and compare it to the current icon iterated by the loop. If the current icon is larger than the temporary one then you assign the current icon to the temporary variable instead and let the loop continue. -- Doing it this way will result in the temporary variable containing the largest icon it could find in the `LargeIcons` array. – Visual Vincent Aug 03 '17 at 16:54
  • @VisualVincent i'm useing as mesagebox to tell me what res the icon is, and all i'm getting it 32x32, here the code for the mesagebox `MsgBox(Item & vbNewLine & "Large" & vbNewLine & LargeIcon.Size.ToString)` also can you post the code for the largeicon part i don't get it. – Gamer_sa22 Aug 03 '17 at 23:53
  • Hmm, I can see your issue. I've used this code before but for some reason it is no longer working... I'll have to look into that. – Visual Vincent Aug 04 '17 at 00:38
  • Here's an alternative solution for you: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a187f98b-9c16-455b-8e69-642f76447b7d/how-to-extract-large-icon-more-than-32-x-32-from-exe-and-dll?forum=vbgeneral – Visual Vincent Aug 04 '17 at 00:48

0 Answers0