Results 1 to 22 of 22

Thread: [RESOLVED] Save Extracted Icon problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Resolved [RESOLVED] Save Extracted Icon problem

    Hi ,

    I have Extracted an Icon from an .exe
    its displayed on a PictureBox after i want to save it i get an exception
    invalid property value

    Code:
    Private Sub Command1_Click()
    
    Dim hIcon As Long
    Picture1.AutoRedraw = True
    hIcon = ExtractAssociatedIcon(App.hInstance, "C:\IconChan.exe", 0)
    DrawIcon Picture1.hdc, 0, 0, hIcon
    
    Picture1.Refresh
    
    SavePicture Picture1.Picture, "C:\Icon1.ico"
    
    
    End Sub
    
    Public Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
    Public Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Thanks !
    Last edited by killer7k; Mar 20th, 2008 at 05:02 PM.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Save Extracted Icon problem

    did you try,
    SavePicture Picture1.Image, "C:\Icon1.ico"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Thanks m8 It work but it look very bad when it saved
    is there any way to save the same icon extracted from a file
    without going for Picturebox ?

    what i mean extract it & save it like it was !

    Thanks !

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Save Extracted Icon problem

    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Thanks koolsid ,

    The problem i get it's strange havent tested your link yet
    but i modified my code & it display good
    but the strange is that it doesnt save as a real icons ?!!

    i will clarifie the situation
    i have a programme that extract icone & change icon

    change icon work very well
    extract icon i have a bit problem with it , they extract icon good but save them in not a real icon

    How ?

    i have some icons in my pc when i load a programme & i choose for him a new icon : result = programme changed with new icons

    but when i use the extracted icon as an icone for the programme
    result = programme changed with a black icone (like icon for svchost.exe) not the real one

    Strange !!!!

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save Extracted Icon problem

    Reason why it isn't a true icon is that if you are saving the .Image property, you will only get bmps, regardless of whatever extension you give the file name. However, if the .Picture property was an icon, than VB will save the .Picture property in actual icon format.

    Saving an icon handle to a file can be done a couple different ways:
    1. GetIconInfo API, write the icon header & then write the icon pixels. However, this requires knowledge of the icon file structure.

    2. Luckily we can let VB do it for us with an API call & custom function.
    Code:
    ' declarations
    Private Type PictDesc
        Size As Long
        Type As Long
        hHandle As Long
        hPal As Long
    End Type
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, _
        riid As Any, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
    
    ' function
    Public Function HandleToStdPicture(ByVal hImage As Long, ByVal imgType As Long) As IPicture
    
        ' function creates a stdPicture object from an image handle (bitmap or icon)
        ' pass vbPicTypeBitmap if handle is bitmap or vbPicTypeIcon if it is an icon
        
        Dim lpPictDesc As PictDesc, aGUID(0 To 3) As Long
        With lpPictDesc
            .Size = Len(lpPictDesc)
            .Type = imgType
            .hHandle = hImage
            .hPal = 0
        End With
        ' IPicture GUID {7BF80980-BF32-101A-8BBB-00AA00300CAB}
        aGUID(0) = &H7BF80980
        aGUID(1) = &H101ABF32
        aGUID(2) = &HAA00BB8B
        aGUID(3) = &HAB0C3000
        ' create stdPicture
        Call OleCreatePictureIndirect(lpPictDesc, aGUID(0), True, HandleToStdPicture)
        
    End Function
    Example:
    Code:
    ' assume an icon handle is stored in hIcon returned from ExtractAssociatedIcon
    
        Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        SavePicture tmpPic, [path\filename]
    Edited: I haven't tested this to see if it would actually work with XP alpha icons, though it might, but VB won't display these correctly anyway, without some API help (that's a different topic).
    Last edited by LaVolpe; Mar 20th, 2008 at 07:39 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    hi LaVolpe,
    I tried your example with my code but i get an exception invalid property value

    What's wrong ?
    Code:
     Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        SavePicture tmpPic, [path\filename]
    Thanks !

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Save Extracted Icon problem

    Did you change [path/file name] to a path or file name?

    ex:

    vb Code:
    1. Dim tmpPic As StdPicture
    2. Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
    3. SavePicture tmpPic, "C:\test.ico"

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save Extracted Icon problem

    Quote Originally Posted by killer7k
    hi LaVolpe,
    I tried your example with my code but i get an exception invalid property value
    What's wrong ?
    Ensure hIcon contains an icon handle from your routines. Doesn't hurt to test result:
    Code:
    Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        If tmpPic Is Nothing Then
             MsgBox "Invalid Image Format or Format Not Supported", vbOkOnly, "Error"
        Else
             SavePicture tmpPic, [path\filename]
        End If
    Last edited by LaVolpe; Mar 20th, 2008 at 09:47 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Quote Originally Posted by DigiRev
    Did you change [path/file name] to a path or file name?

    ex:

    vb Code:
    1. Dim tmpPic As StdPicture
    2. Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
    3. SavePicture tmpPic, "C:\test.ico"
    No i changed to an extention
    come on DigiRev !!!!

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Quote Originally Posted by LaVolpe
    Ensure hIcon contains an icon handle from your routines. Doesn't hurt to test result:
    Code:
    Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        If tmpPic Is Nothing Then
             MsgBox "Invalid Image Format or Format Not Supported", vbOkOnly, "Error"
        Else
             SavePicture tmpPic, [path\filename]
        End If
    Hi LaVolpe

    Here You my code maybe i am missing something

    Code:
    Private Sub Command1_Click()
    CommonDialog1.DialogTitle = "Extract File"
    CommonDialog1.Filter = "All supported files|*.exe;*.dll;*.ico,*.bmp|Executables (*.exe)|*.exe|DLL Files (*.dll)|*.dll|Ico Files (*.ico)|*.ico|Bitmap(*.bmp)|*.bmp|All Files (*.*)|*.*"
    CommonDialog1.ShowOpen
    Dim hIcon As Long
    Picture1.AutoRedraw = True
    hIcon = ExtractAssociatedIcon(App.hInstance, CommonDialog1.FileName, -1)
    Call DrawIconEx(Picture1.hdc, 0, 0, hIcon, 32, 32, 0, 0, 3)
    
    'This work but doesnt produce a True icon
    'SavePicture Picture1.Image, "C:\Icon1.ico"
    
    Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        If tmpPic Is Nothing Then
             MsgBox "Invalid Image Format or Format Not Supported", vbOKOnly, "Error"
        Else
             SavePicture tmpPic, "C:\Iconmania.ico"
        End If
    
    End Sub
    Code:
    Public Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
    Public Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
    
    Public Function HandleToStdPicture(ByVal hImage As Long, ByVal imgType As Long) As IPicture
    
    End Function

    Thanks m8 !

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save Extracted Icon problem

    As I mentioned in #6 above, this may not work with (24 & 32 bit icons) XP-type icons. If you can add the source icon to your form's Icon property, then this should work. But if VB says no, then this won't work either and you will have to create the icon the old fashioned way -- write it yourself.

    P.S. I used exactly your code and ran it on about a dozen files; worked every time.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Hi LaVolpe ,

    I Tried 16*16 also doesnt work !

    you said I used exactly your code and ran it on about a dozen files; worked every time.

    you get worked what ?

    Thanks !
    Last edited by killer7k; Mar 21st, 2008 at 09:04 AM.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save Extracted Icon problem

    For every file I chose, on my C:\ drive, an icon was created as Iconmania.ico in a true ico file format. Which executable(s) do not work for you? Maybe I can replicate the problem. And what exactly is not working?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Can you put your working code again
    i have always the msgbox error
    Stange !!

  16. #16
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save Extracted Icon problem

    Here is your project along with the function I provided. Add a commondialog, picturebox & command button to your new form & play.
    Code:
    Option Explicit
    ' declarations
    Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
    Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
    Private Type PictDesc
        Size As Long
        Type As Long
        hHandle As Long
        hPal As Long
    End Type
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, _
        riid As Any, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
    
    Private Sub Command1_Click()
        CommonDialog1.DialogTitle = "Extract File"
        CommonDialog1.Filter = "All supported files|*.exe;*.dll;*.ico,*.bmp|Executables (*.exe)|*.exe|DLL Files (*.dll)|*.dll|Ico Files (*.ico)|*.ico|Bitmap(*.bmp)|*.bmp|All Files (*.*)|*.*"
        CommonDialog1.ShowOpen
        Dim hIcon As Long
        Picture1.AutoRedraw = True
        Picture1.Cls
        hIcon = ExtractAssociatedIcon(App.hInstance, CommonDialog1.FileName, -1)
        Call DrawIconEx(Picture1.hdc, 0, 0, hIcon, 32, 32, 0, 0, 3)
    
        Dim tmpPic As StdPicture
        Set tmpPic = HandleToStdPicture(hIcon, vbPicTypeIcon)
        If tmpPic Is Nothing Then
             MsgBox "Invalid Image Format or Format Not Supported", vbOKOnly, "Error"
        Else
             SavePicture tmpPic, "C:\Iconmania.ico"
        End If
    
    End Sub
    
    Private Function HandleToStdPicture(ByVal hImage As Long, ByVal imgType As Long) As IPicture
    
        ' function creates a stdPicture object from an image handle (bitmap or icon)
        ' pass vbPicTypeBitmap if handle is bitmap or vbPicTypeIcon if it is an icon
        
        Dim lpPictDesc As PictDesc, aGUID(0 To 3) As Long
        With lpPictDesc
            .Size = Len(lpPictDesc)
            .Type = imgType
            .hHandle = hImage
            .hPal = 0
        End With
        ' IPicture GUID {7BF80980-BF32-101A-8BBB-00AA00300CAB}
        aGUID(0) = &H7BF80980
        aGUID(1) = &H101ABF32
        aGUID(2) = &HAA00BB8B
        aGUID(3) = &HAB0C3000
        ' create stdPicture
        Call OleCreatePictureIndirect(lpPictDesc, aGUID(0), True, HandleToStdPicture)
        
    End Function
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Save Extracted Icon problem

    Yes m8 Working Great Now

    Thanks again !

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Save Extracted Icon problem

    I used the code from post #16. It saved the icon but it came out bad. It had gray areas where it should have white. It shows in the picturebox correctly


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  19. #19
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Save Extracted Icon problem

    I gave you link in your other thread to a more reliable way to save icons by handle. Also in that link, the reason for the poor quality is briefly explained: VB limitation
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Save Extracted Icon problem

    I think I got here from a link you gave in my other thread


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Save Extracted Icon problem

    Quote Originally Posted by jmsrickland View Post
    I think I got here from a link you gave in my other thread
    Yes, I gave you two links. This one and another one. Both can save icon handles to file, but the solution here is limited though much smaller code needed. The other link requires much more code and is only limited to icons 256x256 and smaller.

    Note to self: Update that code to support larger icons. Win10 allows 768x768 icons.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Save Extracted Icon problem

    OK, my end is resolved from your other link so I wont worry about this one. Thanks


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width