Home All Groups Group Topic Archive Search About

Create Icon File From Resource

Author
24 May 2009 4:38 PM
Lorin
I want to create an icon file from resource.
I want to run this from Sub Main.
I have tried several methods and all error out.

Here is one failed example.
Verified that FLAME is in resources under Icon.

    Dim picIcon As PictureBox
    picIcon.Picture = LoadResPicture("FLAME", vbResIcon)      ' FAILS HERE
Object variable or With block variable not set
    picIcon.Refresh
    m_sDPIcon = BuildPath(m_sIconFolder, cIconNameFlame0)
    SavePicture picIcon, m_sDPIcon
    Set picIcon = Nothing

How do I do this?

I also tried to Dim As StdPicture but failed with that too.
I cannot find any Help for StdPicture.
Where is that?

Author
24 May 2009 5:01 PM
Larry Serflaten
"Lorin" <lor***@hotmail.com> wrote
> I have tried several methods and all error out.

Two suggestions:


>     picIcon.Picture = LoadResPicture("FLAME", vbResIcon)      ' FAILS HERE

1.  Use Set.
  Set picIcon.Picture = ...

2.  Use an Image control.

See if either of those get you farther along....

LFS
Author
24 May 2009 8:33 PM
MikeD
Show quote Hide quote
"Lorin" <lor***@hotmail.com> wrote in message
news:C2792067-EA26-4131-98FD-3817532C538D@microsoft.com...
>I want to create an icon file from resource.
> I want to run this from Sub Main.
> I have tried several methods and all error out.
>
> Here is one failed example.
> Verified that FLAME is in resources under Icon.
>
>    Dim picIcon As PictureBox
>    picIcon.Picture = LoadResPicture("FLAME", vbResIcon)      ' FAILS HERE
> Object variable or With block variable not set
>    picIcon.Refresh
>    m_sDPIcon = BuildPath(m_sIconFolder, cIconNameFlame0)
>    SavePicture picIcon, m_sDPIcon
>    Set picIcon = Nothing
>
> How do I do this?

If doing this from Sub Main, you'd need to first load a form which has a
PictureBox control on it.  Then, if you were going to use a local variable
for that PictureBox (no real reason to though), you'd need to set a
reference.  You cannot instantiate a "new" PictureBox control (you get an
"Invalid use of New keyword" compile-time error), but you *can* use
<formreference>.Controls.Add to dynamically add a PictureBox to a form.
Either way, however, you must have a loaded form.

>
> I also tried to Dim As StdPicture but failed with that too.
> I cannot find any Help for StdPicture.
> Where is that?
>

StdPicture is an object so you need to instantiate it.

Dim oStdPicture As StdPicture
Set oStdPicture = New StdPicture

The problem here is that StdPicture doesn't have any way of saving to a
file. So, you'd still need to use a PictureBox or API functions to save the
icon resource as an ICO file.

No idea why you can't find StdPicture in MSDN Library because it's in there.
Just type "StdPicture" into the find field of the Index tab.  You could also
look it up in Object Browser to see the properties and methods that it has.

Another option is to use LoadResData to assign the icon resource's binary
data to a Byte array and then use VB's file I/O statements and functions to
save the file. I don't know if you'd have to do anything more such as
writing header information to the file first. The icon resource might
already include that information.  I don't know for sure about that but I do
know that ICO file's have a header. If you do need to write a header, you'd
need to read up on the ICO file format so you knew what information to
include in the header.

I'm curious why you need to do this.  The whole point of putting icons,
bitmaps, or anything else into a resource that is then compiled into your
program is so you don't need the individual ICO, BMP, or whatever files. If
you must have this icon as an ICO file, it might be better for you to
distribute the ICO file with your app and install it as part of your app's
installation. Of course, the risk then is that a user could potentially
delete the file. But if it's installed in the application's installation
folder, that's not likely (and re-installing would fix it).

--
Mike
Author
25 May 2009 2:47 PM
Lorin
Thanks for the responses.
No StdPicture is not there.  Maybe you have a MSDN version later than the
last VB version (was it Oct 2001 ?).
I have later MSDNs too but was not sure if installing would benefit.
I went to MS site and found a very short blurb on it and it was just enough
to see what was wrong.

(1) should I install the latest MSDN? or not and why?

This is what finally worked.
    Dim picIcon As StdPicture
    Set picIcon = LoadResPicture("FLAME", vbResIcon)
Note:  the Set and no .Picture, per MS website
    m_sDPAssocIcon = BuildPath(m_sIconFolder, cIconNameFlame0)
    SavePicture picIcon, m_sDPAssocIcon
    Set picIcon = Nothing

Why? I need a file icon to use in the routine that follows that assigns my
icon to the files associated with my app.
I do not know how to do that otherwise (yet).

(2) is there a way to assign associated an icon without using an icon file?

(3) I am using a standard icon that works properly on the Caption Bar,
however, as a icon on a file it has a black background. (tested in Vista
only so far)  On the caption bar its background is transparent.  What is
wrong with my icon?  What are the restrictions for the file icon?  I tried
to web search this but found nothing.

I will probably post these new questions if I do not get responses here.
Not sure if posting new questions here is the right thing to do.



Show quoteHide quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:Oeojg7K3JHA.1372@TK2MSFTNGP05.phx.gbl...
>
> "Lorin" <lor***@hotmail.com> wrote in message
> news:C2792067-EA26-4131-98FD-3817532C538D@microsoft.com...
>>I want to create an icon file from resource.
>> I want to run this from Sub Main.
>> I have tried several methods and all error out.
>>
>> Here is one failed example.
>> Verified that FLAME is in resources under Icon.
>>
>>    Dim picIcon As PictureBox
>>    picIcon.Picture = LoadResPicture("FLAME", vbResIcon)      ' FAILS HERE
>> Object variable or With block variable not set
>>    picIcon.Refresh
>>    m_sDPIcon = BuildPath(m_sIconFolder, cIconNameFlame0)
>>    SavePicture picIcon, m_sDPIcon
>>    Set picIcon = Nothing
>>
>> How do I do this?
>
> If doing this from Sub Main, you'd need to first load a form which has a
> PictureBox control on it.  Then, if you were going to use a local variable
> for that PictureBox (no real reason to though), you'd need to set a
> reference.  You cannot instantiate a "new" PictureBox control (you get an
> "Invalid use of New keyword" compile-time error), but you *can* use
> <formreference>.Controls.Add to dynamically add a PictureBox to a form.
> Either way, however, you must have a loaded form.
>
>>
>> I also tried to Dim As StdPicture but failed with that too.
>> I cannot find any Help for StdPicture.
>> Where is that?
>>
>
> StdPicture is an object so you need to instantiate it.
>
> Dim oStdPicture As StdPicture
> Set oStdPicture = New StdPicture
>
> The problem here is that StdPicture doesn't have any way of saving to a
> file. So, you'd still need to use a PictureBox or API functions to save
> the icon resource as an ICO file.
>
> No idea why you can't find StdPicture in MSDN Library because it's in
> there. Just type "StdPicture" into the find field of the Index tab.  You
> could also look it up in Object Browser to see the properties and methods
> that it has.
>
> Another option is to use LoadResData to assign the icon resource's binary
> data to a Byte array and then use VB's file I/O statements and functions
> to save the file. I don't know if you'd have to do anything more such as
> writing header information to the file first. The icon resource might
> already include that information.  I don't know for sure about that but I
> do know that ICO file's have a header. If you do need to write a header,
> you'd need to read up on the ICO file format so you knew what information
> to include in the header.
>
> I'm curious why you need to do this.  The whole point of putting icons,
> bitmaps, or anything else into a resource that is then compiled into your
> program is so you don't need the individual ICO, BMP, or whatever files.
> If you must have this icon as an ICO file, it might be better for you to
> distribute the ICO file with your app and install it as part of your app's
> installation. Of course, the risk then is that a user could potentially
> delete the file. But if it's installed in the application's installation
> folder, that's not likely (and re-installing would fix it).
>
> --
> Mike
>
>
Author
25 May 2009 3:40 PM
MikeD
"Lorin" <lor***@hotmail.com> wrote in message
news:5DEECC66-C23A-4CE2-85F3-62ABF62AF108@microsoft.com...

>
> Why? I need a file icon to use in the routine that follows that assigns my
> icon to the files associated with my app.
> I do not know how to do that otherwise (yet).
>
> (2) is there a way to assign associated an icon without using an icon
> file?

Yep.  Use your EXE file and specify the index for the specific icon you
want. For an example, run REGEDIT and take a look at some other file
extensions under HKEY_CLASSES_ROOT....txtfile would be a good one to look
at.  Notice the DefaultIcon key.

--
Mike
Author
25 May 2009 8:21 PM
Nobody
"Lorin" <lor***@hotmail.com> wrote in message
news:5DEECC66-C23A-4CE2-85F3-62ABF62AF108@microsoft.com...
> (1) should I install the latest MSDN? or not and why?

No. Oct 2001 is the latest that integrates with VB6. I don't think that
later MSDN have VB6 help. I have the 2003 edition, and VB6 help is not part
of it.
Author
26 May 2009 12:02 AM
mayayana
> (3) I am using a standard icon that works properly on the Caption Bar,
> however, as a icon on a file it has a black background. (tested in Vista
> only so far)  On the caption bar its background is transparent.  What is
> wrong with my icon?  What are the restrictions for the file icon?  I tried
> to web search this but found nothing.
>
  In addition to assigning an indexed icon from your EXE,
you could also ust save the file as a custom resource
and write the bytes to disk.

  On the black background: That's the mask bitmap showing.
I've seen that on XP when it wasn't a problem on Win9x.
I don't remember the exact cause. I think it was that
there was a wrong byte in the header that Win9x ignored
but which confused XP. You might try comparing the icon
header (ICONDIR and ICONDIRENTRY(s)) with a valid
icon to see if there's a problem with the standard values.

  Also check the icon that you originally put into the
resource, to see if that displays OK. It may be getting
corrupted in its trip through the PictureBox. (I know that
the docs say SavePicture will save an icon if an icon was
loaded, but I'm not certain that's true.)


Show quoteHide quote
> I will probably post these new questions if I do not get responses here.
> Not sure if posting new questions here is the right thing to do.
>
>
>
> "MikeD" <nob***@nowhere.edu> wrote in message
> news:Oeojg7K3JHA.1372@TK2MSFTNGP05.phx.gbl...
> >
> > "Lorin" <lor***@hotmail.com> wrote in message
> > news:C2792067-EA26-4131-98FD-3817532C538D@microsoft.com...
> >>I want to create an icon file from resource.
> >> I want to run this from Sub Main.
> >> I have tried several methods and all error out.
> >>
> >> Here is one failed example.
> >> Verified that FLAME is in resources under Icon.
> >>
> >>    Dim picIcon As PictureBox
> >>    picIcon.Picture = LoadResPicture("FLAME", vbResIcon)      ' FAILS
HERE
> >> Object variable or With block variable not set
> >>    picIcon.Refresh
> >>    m_sDPIcon = BuildPath(m_sIconFolder, cIconNameFlame0)
> >>    SavePicture picIcon, m_sDPIcon
> >>    Set picIcon = Nothing
> >>
> >> How do I do this?
> >
> > If doing this from Sub Main, you'd need to first load a form which has a
> > PictureBox control on it.  Then, if you were going to use a local
variable
> > for that PictureBox (no real reason to though), you'd need to set a
> > reference.  You cannot instantiate a "new" PictureBox control (you get
an
> > "Invalid use of New keyword" compile-time error), but you *can* use
> > <formreference>.Controls.Add to dynamically add a PictureBox to a form.
> > Either way, however, you must have a loaded form.
> >
> >>
> >> I also tried to Dim As StdPicture but failed with that too.
> >> I cannot find any Help for StdPicture.
> >> Where is that?
> >>
> >
> > StdPicture is an object so you need to instantiate it.
> >
> > Dim oStdPicture As StdPicture
> > Set oStdPicture = New StdPicture
> >
> > The problem here is that StdPicture doesn't have any way of saving to a
> > file. So, you'd still need to use a PictureBox or API functions to save
> > the icon resource as an ICO file.
> >
> > No idea why you can't find StdPicture in MSDN Library because it's in
> > there. Just type "StdPicture" into the find field of the Index tab.  You
> > could also look it up in Object Browser to see the properties and
methods
> > that it has.
> >
> > Another option is to use LoadResData to assign the icon resource's
binary
> > data to a Byte array and then use VB's file I/O statements and functions
> > to save the file. I don't know if you'd have to do anything more such as
> > writing header information to the file first. The icon resource might
> > already include that information.  I don't know for sure about that but
I
> > do know that ICO file's have a header. If you do need to write a header,
> > you'd need to read up on the ICO file format so you knew what
information
> > to include in the header.
> >
> > I'm curious why you need to do this.  The whole point of putting icons,
> > bitmaps, or anything else into a resource that is then compiled into
your
> > program is so you don't need the individual ICO, BMP, or whatever files.
> > If you must have this icon as an ICO file, it might be better for you to
> > distribute the ICO file with your app and install it as part of your
app's
> > installation. Of course, the risk then is that a user could potentially
> > delete the file. But if it's installed in the application's installation
> > folder, that's not likely (and re-installing would fix it).
> >
> > --
> > Mike
> >
> >
>