Results 1 to 8 of 8

Thread: Icon background color and size.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    How come when I extract an icon to a picture box and use the SavePicture method, the back ground of the file.ico is black and the image is smaller?

    Picture box be transparent?
    Mako Shark
    Great White

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You extract an icon from a file and paint it to a picturebox and then you save the bitmap by using SavePicture, right?
    What you save is a BMP file and not the icon.

    If you want to convert the bitmap you have painted on the picture box back to an icon then do the following:[*]Add an ImageList control to the Form[*]Set the MaskColor of the ImageList to the same as the BackColor of the PictureBox[*]When you have drawn the icon on the PictureBox add the image to the ImageList ListImages collection.[*]Clear the PictureBox[*]Use the ExtractIcon method of the ImageList ListImages collection to pull the image back from the ImageList to the PictureBox[*]Now you have an icon in the PictureBox save it with the SavePicture function
    Code:
    ImageList1.ListImages.Add , "Icon", Picture1.Image
    Picture1.Cls
    Picture1 = ImageList1.ListImages("Icon").ExtractIcon
    ImageList1.ListImages.Clear
    SavePicture Picture1, "c:\MyIcon.ico"
    Good luck!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    The background is still black. The picture lost its size and became distorted too.
    Mako Shark
    Great White

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    How do you extract the icon and how are you painting it to the picture box?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    I got this somewhere on this site.


    Code:
    Option Explicit
    
    Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
    Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    
    Private Sub Form_Load()
        'Set initial settings
        Picture1.AutoRedraw = True
        Me.ScaleMode = vbPixels
    End Sub
    
    Private Sub Command1_Click()
        Dim hIcon As Long
        'Extract the icon from Calculator
        hIcon = ExtractIcon(App.hInstance, "C:\myproject.exe", 0)
        
        'Draw the Icon on to Picture1
        DrawIcon Picture1.hdc, 0, 0, hIcon
        Picture1.Picture = Picture1.Image
        
        'Convert the Bitmap to an Icon
        ImageList1.ListImages.Add , , Picture1.Picture
        Picture1.Cls
        Set Picture1.Picture = Nothing
        Set Picture1.Picture = ImageList1.ListImages(1).ExtractIcon
        'Save the Icon
        SavePicture Picture1.Picture, App.Path & "\MyIco.ico"
    End Sub
    Thanks Joacim for always helping.
    Mako Shark
    Great White

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Make sure the picture box is 32x32 pixels in size (or 36x36 if it has a border).

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    That did the trick for some icons. There are others that are smaller so I have to change the imagelist size.

    Thanks.

    I think I have to play around with the size in the imagelist.
    Mako Shark
    Great White

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well, ExtractIcon always extracts the 32x32 icon.
    If you want to extract icons in the size of 16x16 use ExtractIconEx instead.

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