ExtractIconEx 
Author Message
 ExtractIconEx

Does anybody know how to properly convert an XP Icon to bitmap with all the
Alpha Data intact.
If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
displayed as it should be.
If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap) and
this Icon has shadows then the shadows are displayed as Dark (almost black)
pixels.
What is more annoying is if I extract the Icon to a File I can then use
Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot use:
    MyIcon = New Icon("MyFile.Ico")
    MyBitmap = Bitmap(MyIcon)

I've tried everything I can think of. I have even assigned the Icon to an
Imagelist, there was a slight improvement there, but it was still not right.

Q:    Why do I want to convert to a Bitmap?
A:    So I can assign it to an Image property in Code. If I assign an Icon
to the Image Property at design time that works fine, but at runtime I
cannot assign an Icon to a property expecting an Image, I have to use
Icon.ToBitmap which does not do the same thing.

Google turned up nothing of any use.

Use a Manifest --> Already using
Bitmap.MakeTransparent --> Does nothing.



Sun, 11 Sep 2005 01:26:02 GMT  
 ExtractIconEx
Hello Mick,


Quote:
> Does anybody know how to properly convert an XP Icon to bitmap with
all the
> Alpha Data intact.
> If I use DrawIcon with the icon returned from ExtractIconEx the Icon
is
> displayed as it should be.
> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
and
> this Icon has shadows then the shadows are displayed as Dark (almost
black)
> pixels.
> What is more annoying is if I extract the Icon to a File I can then
use
> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
use:
>     MyIcon = New Icon("MyFile.Ico")
>     MyBitmap = Bitmap(MyIcon)

IMHO bitmap files do not support transparency.

Regards,
Herfried K. Wagner



Sun, 11 Sep 2005 02:20:21 GMT  
 ExtractIconEx
Have you actually checked the Bitmap bits to see if the alpha channel exists? Should be able to load the bits after calling LockBitmapBits. Just reply if you want some code on how to do this (it will take some searching to find the project or I would post it now).

It the Alpha channel exists, then you know the problem is in the display device.

Jeremy

Quote:

> Does anybody know how to properly convert an XP Icon to bitmap with all the
> Alpha Data intact.
> If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
> displayed as it should be.
> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap) and
> this Icon has shadows then the shadows are displayed as Dark (almost black)
> pixels.
> What is more annoying is if I extract the Icon to a File I can then use
> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot use:
>     MyIcon = New Icon("MyFile.Ico")
>     MyBitmap = Bitmap(MyIcon)

> I've tried everything I can think of. I have even assigned the Icon to an
> Imagelist, there was a slight improvement there, but it was still not right.

> Q:    Why do I want to convert to a Bitmap?
> A:    So I can assign it to an Image property in Code. If I assign an Icon
> to the Image Property at design time that works fine, but at runtime I
> cannot assign an Icon to a property expecting an Image, I have to use
> Icon.ToBitmap which does not do the same thing.

> Google turned up nothing of any use.

> Use a Manifest --> Already using
> Bitmap.MakeTransparent --> Does nothing.



Sun, 11 Sep 2005 03:48:30 GMT  
 ExtractIconEx
Incorrect. Bitmaps DO support an alpha channel:

Dim b as new Drawing.Bitmap(10,10,Drawing.Imaging.PixelFormat.Format32bppArgb )

I dont know how to fix the Icon problem though. sorry.


Quote:
> Hello Mick,


> > Does anybody know how to properly convert an XP Icon to bitmap with
> all the
> > Alpha Data intact.
> > If I use DrawIcon with the icon returned from ExtractIconEx the Icon
> is
> > displayed as it should be.
> > If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
> and
> > this Icon has shadows then the shadows are displayed as Dark (almost
> black)
> > pixels.
> > What is more annoying is if I extract the Icon to a File I can then
> use
> > Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
> use:
> >     MyIcon = New Icon("MyFile.Ico")
> >     MyBitmap = Bitmap(MyIcon)

> IMHO bitmap files do not support transparency.

> Regards,
> Herfried K. Wagner



Sun, 11 Sep 2005 03:44:58 GMT  
 ExtractIconEx
I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
start. Whilst programming in VB6 I never had the need to play around with
Bitmap bits and so never learned anything about constructing bitmaps or
extracting information about their structure. Whenever I wanted to do
something like this I would simply BitBlt or TransparentBlt to the dc. Then
again I never had XP Icons to play with so that solution may not have worked
either.

If you find the project I would be interested in some code. I'll just go
through google and search for bitmap manipulation in VB\VC and see if that
sheds any light.

The Image is drawn correctly if I use DrawIcon in a Picturebox's Graphics
object, but not in a Bitmaps Graphics object. When I assign an XP Icon to an
Image property at design time I have noticed that it's RawFormat is changed
to PNG. Maybe that's the answer, but I think that is just the default format
for a new bitmap. The other obvious difference is that the image is stored
in the resx file, but I don't know how this is going to help me.

Thanks for any help.

Mick


Quote:
>Have you actually checked the Bitmap bits to see if the alpha channel

exists? Should be able to load the bits after calling LockBitmapBits. Just
reply if you want >some code on how to do this (it will take some searching
to find the project or I would post it now).
Quote:

>It the Alpha channel exists, then you know the problem is in the display
device.

>Jeremy




Quote:
>> Does anybody know how to properly convert an XP Icon to bitmap with all
the
>> Alpha Data intact.
>> If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
>> displayed as it should be.
>> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap) and
>> this Icon has shadows then the shadows are displayed as Dark (almost
black)
>> pixels.
>> What is more annoying is if I extract the Icon to a File I can then use
>> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot use:
>>     MyIcon = New Icon("MyFile.Ico")
>>     MyBitmap = Bitmap(MyIcon)

>> I've tried everything I can think of. I have even assigned the Icon to an
>> Imagelist, there was a slight improvement there, but it was still not
right.

>> Q:    Why do I want to convert to a Bitmap?
>> A:    So I can assign it to an Image property in Code. If I assign an
Icon
>> to the Image Property at design time that works fine, but at runtime I
>> cannot assign an Icon to a property expecting an Image, I have to use
>> Icon.ToBitmap which does not do the same thing.

>> Google turned up nothing of any use.

>> Use a Manifest --> Already using
>> Bitmap.MakeTransparent --> Does nothing.



Sun, 11 Sep 2005 06:53:23 GMT  
 ExtractIconEx
Well, I found the project, but I dont think thats the problem. The following almost works fine for me:

'//////////////////////////////////////////
Dim bmpSource As Bitmap

Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My Documents\Graphics\icons\Macromedia\Flash\union.ico")

Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

'// From HICON

bmpSource = Bitmap.FromHicon(ico.Handle)

'bmpSource = ico.ToBitmap

g.DrawImage(bmpSource, 0, 0)

g.Dispose()

'////////////////////////////////////////////

The only exception is that the icon looks a bit too small when I use the graphics object to draw it. If I set the Image property of picMain, it looks perfect. Can you post your code? I have also posted the code to get the bitmap bits and load them into an array:

Dim bdSrc As Imaging.BitmapData

Dim lArrayLength As Long

Dim rTemp As New Rectangle(0, 0, 1, 1)

Dim bSrc() As Byte

rTemp.Size = bmpSrc.Size

lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

'// resize the array to fit bits

ReDim bSrc(lArrayLength)

'// lock the memory

bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite, bmpSrc.PixelFormat)

'// copy to managed mem

Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

'// ...process bitarray...

'// move back into unmanaged memory

Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

'// restore

bmpSrc.UnlockBits(bdSrc)

Jeremy

Quote:

> I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
> start. Whilst programming in VB6 I never had the need to play around with
> Bitmap bits and so never learned anything about constructing bitmaps or
> extracting information about their structure. Whenever I wanted to do
> something like this I would simply BitBlt or TransparentBlt to the dc. Then
> again I never had XP Icons to play with so that solution may not have worked
> either.

> If you find the project I would be interested in some code. I'll just go
> through google and search for bitmap manipulation in VB\VC and see if that
> sheds any light.

> The Image is drawn correctly if I use DrawIcon in a Picturebox's Graphics
> object, but not in a Bitmaps Graphics object. When I assign an XP Icon to an
> Image property at design time I have noticed that it's RawFormat is changed
> to PNG. Maybe that's the answer, but I think that is just the default format
> for a new bitmap. The other obvious difference is that the image is stored
> in the resx file, but I don't know how this is going to help me.

> Thanks for any help.

> Mick



> >Have you actually checked the Bitmap bits to see if the alpha channel
> exists? Should be able to load the bits after calling LockBitmapBits. Just
> reply if you want >some code on how to do this (it will take some searching
> to find the project or I would post it now).

> >It the Alpha channel exists, then you know the problem is in the display
> device.

> >Jeremy



> >> Does anybody know how to properly convert an XP Icon to bitmap with all
> the
> >> Alpha Data intact.
> >> If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
> >> displayed as it should be.
> >> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap) and
> >> this Icon has shadows then the shadows are displayed as Dark (almost
> black)
> >> pixels.
> >> What is more annoying is if I extract the Icon to a File I can then use
> >> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot use:
> >>     MyIcon = New Icon("MyFile.Ico")
> >>     MyBitmap = Bitmap(MyIcon)

> >> I've tried everything I can think of. I have even assigned the Icon to an
> >> Imagelist, there was a slight improvement there, but it was still not
> right.

> >> Q:    Why do I want to convert to a Bitmap?
> >> A:    So I can assign it to an Image property in Code. If I assign an
> Icon
> >> to the Image Property at design time that works fine, but at runtime I
> >> cannot assign an Icon to a property expecting an Image, I have to use
> >> Icon.ToBitmap which does not do the same thing.

> >> Google turned up nothing of any use.

> >> Use a Manifest --> Already using
> >> Bitmap.MakeTransparent --> Does nothing.



Sun, 11 Sep 2005 21:44:08 GMT  
 ExtractIconEx
The problem is that I want to set the icon to the Image property of a
dynamically created control, not draw it to a graphics object.
I can draw the icon fine if I draw directly to a graphics object:

 Dim g As Graphics = picMain.CreateGraphics
 g.DrawIcon(Me.Icon, 0, 0)
 g.Dispose()

or were you looking for the ExtractIconEx code:
////////////////////////////////////////////////////////////////////////////
  Imports Microsoft.Win32
....
....
    Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
            (ByVal lpszFile As String, _
            ByVal nIconIndex As Integer, _
            ByVal phiconLarge As IntPtr(), _
            ByVal phiconSmall As IntPtr(), _
            ByVal nIcons As Integer) As Integer

    Private Function ShellIcon(ByVal n As Integer, Optional ByVal Large As
Boolean = False) As Icon

        Dim hLarge() As IntPtr = {New IntPtr}
        Dim hSmall() As IntPtr = {New IntPtr}
        Dim ico As Icon

        'Some users customise their icons. So lets check the registry
        'for custom Icons first.
        Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _
                ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
Icons")

        'Return Shell32 and the Integer Passed in if the Key does not exist
        Dim s As String() = CType(rk.GetValue(n.ToString, "Shell32.dll," &
n), String).Split(",")

        Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

        If Large = True Then
            ico = Icon.FromHandle(hLarge(0))
        Else
            ico = Icon.FromHandle(hSmall(0))
        End If

        'I think this does the same as DestroyIcon.
        hLarge(0) = IntPtr.Zero
        hSmall(0) = IntPtr.Zero

        Return ico

    End Function
////////////////////////////////////////////////////////////////////////////

I have found the following:

Assign the Icon to the Image property at Design Time ----> ImageFormat =
PNG----Looks Good

At runtime:
bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
Icon----Looks Good
bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
MemoryBmp----Looks Very Bad

So it would seem that the key lies in ImageFormat but I don't seem to be
able to assign that, it's ReadOnly.

Now Why has OutLook stopped Indenting the Previous Message?

------Previous
Message---------------------------------------------------------------------
-------------------------


Well, I found the project, but I dont think thats the problem. The following
almost works fine for me:

'//////////////////////////////////////////
Dim bmpSource As Bitmap

Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
Documents\Graphics\icons\Macromedia\Flash\union.ico")

Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

'// From HICON

bmpSource = Bitmap.FromHicon(ico.Handle)

'bmpSource = ico.ToBitmap

g.DrawImage(bmpSource, 0, 0)

g.Dispose()

'////////////////////////////////////////////

The only exception is that the icon looks a bit too small when I use the
graphics object to draw it. If I set the Image property of picMain, it looks
perfect. Can you post your code? I have also posted the code to get the
bitmap bits and load them into an array:

Dim bdSrc As Imaging.BitmapData

Dim lArrayLength As Long

Dim rTemp As New Rectangle(0, 0, 1, 1)

Dim bSrc() As Byte

rTemp.Size = bmpSrc.Size

lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

'// resize the array to fit bits

ReDim bSrc(lArrayLength)

'// lock the memory

bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite,
bmpSrc.PixelFormat)

'// copy to managed mem

Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

'// ...process bitarray...

'// move back into unmanaged memory

Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

'// restore

bmpSrc.UnlockBits(bdSrc)

Jeremy


Quote:
> I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
> start. Whilst programming in VB6 I never had the need to play around with
> Bitmap bits and so never learned anything about constructing bitmaps or
> extracting information about their structure. Whenever I wanted to do
> something like this I would simply BitBlt or TransparentBlt to the dc.
Then
> again I never had XP Icons to play with so that solution may not have
worked
> either.

> If you find the project I would be interested in some code. I'll just go
> through google and search for bitmap manipulation in VB\VC and see if that
> sheds any light.

> The Image is drawn correctly if I use DrawIcon in a Picturebox's Graphics
> object, but not in a Bitmaps Graphics object. When I assign an XP Icon to
an
> Image property at design time I have noticed that it's RawFormat is
changed
> to PNG. Maybe that's the answer, but I think that is just the default
format
> for a new bitmap. The other obvious difference is that the image is stored
> in the resx file, but I don't know how this is going to help me.

> Thanks for any help.

> Mick



> >Have you actually checked the Bitmap bits to see if the alpha channel
> exists? Should be able to load the bits after calling LockBitmapBits. Just
> reply if you want >some code on how to do this (it will take some
searching
> to find the project or I would post it now).

> >It the Alpha channel exists, then you know the problem is in the display
> device.

> >Jeremy



> >> Does anybody know how to properly convert an XP Icon to bitmap with all
> the
> >> Alpha Data intact.
> >> If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
> >> displayed as it should be.
> >> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
and
> >> this Icon has shadows then the shadows are displayed as Dark (almost
> black)
> >> pixels.
> >> What is more annoying is if I extract the Icon to a File I can then use
> >> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
use:
> >>     MyIcon = New Icon("MyFile.Ico")
> >>     MyBitmap = Bitmap(MyIcon)

> >> I've tried everything I can think of. I have even assigned the Icon to
an
> >> Imagelist, there was a slight improvement there, but it was still not
> right.

> >> Q:    Why do I want to convert to a Bitmap?
> >> A:    So I can assign it to an Image property in Code. If I assign an
> Icon
> >> to the Image Property at design time that works fine, but at runtime I
> >> cannot assign an Icon to a property expecting an Image, I have to use
> >> Icon.ToBitmap which does not do the same thing.

> >> Google turned up nothing of any use.

> >> Use a Manifest --> Already using
> >> Bitmap.MakeTransparent --> Does nothing.



Mon, 12 Sep 2005 00:12:39 GMT  
 ExtractIconEx

Quote:
> I can draw the icon fine if I draw directly to a graphics object:

Then what is the problem? Create a new bitmap, create a new graphics object from the bitmap, draw the icon on it using DrawIcon, then set the picture property = to the bitmap. However, I have been unsuccessful in getting the icon to draw properly using DrawIcon .

Jeremy

Quote:

> The problem is that I want to set the icon to the Image property of a
> dynamically created control, not draw it to a graphics object.
> I can draw the icon fine if I draw directly to a graphics object:

>  Dim g As Graphics = picMain.CreateGraphics
>  g.DrawIcon(Me.Icon, 0, 0)
>  g.Dispose()

> or were you looking for the ExtractIconEx code:
> ////////////////////////////////////////////////////////////////////////////
>   Imports Microsoft.Win32
> ....
> ....
>     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
>             (ByVal lpszFile As String, _
>             ByVal nIconIndex As Integer, _
>             ByVal phiconLarge As IntPtr(), _
>             ByVal phiconSmall As IntPtr(), _
>             ByVal nIcons As Integer) As Integer

>     Private Function ShellIcon(ByVal n As Integer, Optional ByVal Large As
> Boolean = False) As Icon

>         Dim hLarge() As IntPtr = {New IntPtr}
>         Dim hSmall() As IntPtr = {New IntPtr}
>         Dim ico As Icon

>         'Some users customise their icons. So lets check the registry
>         'for custom Icons first.
>         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _
>                 ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> Icons")

>         'Return Shell32 and the Integer Passed in if the Key does not exist
>         Dim s As String() = CType(rk.GetValue(n.ToString, "Shell32.dll," &
> n), String).Split(",")

>         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

>         If Large = True Then
>             ico = Icon.FromHandle(hLarge(0))
>         Else
>             ico = Icon.FromHandle(hSmall(0))
>         End If

>         'I think this does the same as DestroyIcon.
>         hLarge(0) = IntPtr.Zero
>         hSmall(0) = IntPtr.Zero

>         Return ico

>     End Function
> ////////////////////////////////////////////////////////////////////////////

> I have found the following:

> Assign the Icon to the Image property at Design Time ----> ImageFormat =
> PNG----Looks Good

> At runtime:
> bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> Icon----Looks Good
> bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> MemoryBmp----Looks Very Bad

> So it would seem that the key lies in ImageFormat but I don't seem to be
> able to assign that, it's ReadOnly.

> Now Why has OutLook stopped Indenting the Previous Message?

> ------Previous
> Message---------------------------------------------------------------------
> -------------------------



> Well, I found the project, but I dont think thats the problem. The following
> almost works fine for me:

> '//////////////////////////////////////////
> Dim bmpSource As Bitmap

> Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> Documents\Graphics\icons\Macromedia\Flash\union.ico")

> Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> '// From HICON

> bmpSource = Bitmap.FromHicon(ico.Handle)

> 'bmpSource = ico.ToBitmap

> g.DrawImage(bmpSource, 0, 0)

> g.Dispose()

> '////////////////////////////////////////////

> The only exception is that the icon looks a bit too small when I use the
> graphics object to draw it. If I set the Image property of picMain, it looks
> perfect. Can you post your code? I have also posted the code to get the
> bitmap bits and load them into an array:

> Dim bdSrc As Imaging.BitmapData

> Dim lArrayLength As Long

> Dim rTemp As New Rectangle(0, 0, 1, 1)

> Dim bSrc() As Byte

> rTemp.Size = bmpSrc.Size

> lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> '// resize the array to fit bits

> ReDim bSrc(lArrayLength)

> '// lock the memory

> bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite,
> bmpSrc.PixelFormat)

> '// copy to managed mem

> Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

> '// ...process bitarray...

> '// move back into unmanaged memory

> Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

> '// restore

> bmpSrc.UnlockBits(bdSrc)

> Jeremy



> > I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
> > start. Whilst programming in VB6 I never had the need to play around with
> > Bitmap bits and so never learned anything about constructing bitmaps or
> > extracting information about their structure. Whenever I wanted to do
> > something like this I would simply BitBlt or TransparentBlt to the dc.
> Then
> > again I never had XP Icons to play with so that solution may not have
> worked
> > either.

> > If you find the project I would be interested in some code. I'll just go
> > through google and search for bitmap manipulation in VB\VC and see if that
> > sheds any light.

> > The Image is drawn correctly if I use DrawIcon in a Picturebox's Graphics
> > object, but not in a Bitmaps Graphics object. When I assign an XP Icon to
> an
> > Image property at design time I have noticed that it's RawFormat is
> changed
> > to PNG. Maybe that's the answer, but I think that is just the default
> format
> > for a new bitmap. The other obvious difference is that the image is stored
> > in the resx file, but I don't know how this is going to help me.

> > Thanks for any help.

> > Mick



> > >Have you actually checked the Bitmap bits to see if the alpha channel
> > exists? Should be able to load the bits after calling LockBitmapBits. Just
> > reply if you want >some code on how to do this (it will take some
> searching
> > to find the project or I would post it now).

> > >It the Alpha channel exists, then you know the problem is in the display
> > device.

> > >Jeremy



> > >> Does anybody know how to properly convert an XP Icon to bitmap with all
> > the
> > >> Alpha Data intact.
> > >> If I use DrawIcon with the icon returned from ExtractIconEx the Icon is
> > >> displayed as it should be.
> > >> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
> and
> > >> this Icon has shadows then the shadows are displayed as Dark (almost
> > black)
> > >> pixels.
> > >> What is more annoying is if I extract the Icon to a File I can then use
> > >> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
> use:
> > >>     MyIcon = New Icon("MyFile.Ico")
> > >>     MyBitmap = Bitmap(MyIcon)

> > >> I've tried everything I can think of. I have even assigned the Icon to
> an
> > >> Imagelist, there was a slight improvement there, but it was still not
> > right.

> > >> Q:    Why do I want to convert to a Bitmap?
> > >> A:    So I can assign it to an Image property in Code. If I assign an
> > Icon
> > >> to the Image Property at design time that works fine, but at runtime I
> > >> cannot assign an Icon to a property expecting an Image, I have to use
> > >> Icon.ToBitmap which does not do the same thing.

> > >> Google turned up nothing of any use.

> > >> Use a Manifest --> Already using
> > >> Bitmap.MakeTransparent --> Does nothing.



Mon, 12 Sep 2005 01:24:26 GMT  
 ExtractIconEx
The problem is that the Graphics object when created from a bitmap does not
fully support the Alpha channels of the Icon. A Picturebox does. It appears
that the Alpha channel of the Memory Bitmap accepts On or Off. So the fully
transparent pixels are not rendered and the semi transparent pixels are
rendered as fully opaque.


Quote:
> I can draw the icon fine if I draw directly to a graphics object:

Then what is the problem? Create a new bitmap, create a new graphics object
from the bitmap, draw the icon on it using DrawIcon, then set the picture
property = to the bitmap. However, I have been unsuccessful in getting the
icon to draw properly using DrawIcon .

Jeremy


Quote:
> The problem is that I want to set the icon to the Image property of a
> dynamically created control, not draw it to a graphics object.
> I can draw the icon fine if I draw directly to a graphics object:

>  Dim g As Graphics = picMain.CreateGraphics
>  g.DrawIcon(Me.Icon, 0, 0)
>  g.Dispose()

> or were you looking for the ExtractIconEx code:

////////////////////////////////////////////////////////////////////////////
Quote:
>   Imports Microsoft.Win32
> ....
> ....
>     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
>             (ByVal lpszFile As String, _
>             ByVal nIconIndex As Integer, _
>             ByVal phiconLarge As IntPtr(), _
>             ByVal phiconSmall As IntPtr(), _
>             ByVal nIcons As Integer) As Integer

>     Private Function ShellIcon(ByVal n As Integer, Optional ByVal Large As
> Boolean = False) As Icon

>         Dim hLarge() As IntPtr = {New IntPtr}
>         Dim hSmall() As IntPtr = {New IntPtr}
>         Dim ico As Icon

>         'Some users customise their icons. So lets check the registry
>         'for custom Icons first.
>         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _
>                 ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> Icons")

>         'Return Shell32 and the Integer Passed in if the Key does not
exist
>         Dim s As String() = CType(rk.GetValue(n.ToString, "Shell32.dll," &
> n), String).Split(",")

>         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

>         If Large = True Then
>             ico = Icon.FromHandle(hLarge(0))
>         Else
>             ico = Icon.FromHandle(hSmall(0))
>         End If

>         'I think this does the same as DestroyIcon.
>         hLarge(0) = IntPtr.Zero
>         hSmall(0) = IntPtr.Zero

>         Return ico

>     End Function

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:

> I have found the following:

> Assign the Icon to the Image property at Design Time ----> ImageFormat =
> PNG----Looks Good

> At runtime:
> bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> Icon----Looks Good
> bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> MemoryBmp----Looks Very Bad

> So it would seem that the key lies in ImageFormat but I don't seem to be
> able to assign that, it's ReadOnly.

> Now Why has OutLook stopped Indenting the Previous Message?

> ------Previous

Message---------------------------------------------------------------------

- Show quoted text -

Quote:
> -------------------------



> Well, I found the project, but I dont think thats the problem. The
following
> almost works fine for me:

> '//////////////////////////////////////////
> Dim bmpSource As Bitmap

> Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> Documents\Graphics\icons\Macromedia\Flash\union.ico")

> Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> '// From HICON

> bmpSource = Bitmap.FromHicon(ico.Handle)

> 'bmpSource = ico.ToBitmap

> g.DrawImage(bmpSource, 0, 0)

> g.Dispose()

> '////////////////////////////////////////////

> The only exception is that the icon looks a bit too small when I use the
> graphics object to draw it. If I set the Image property of picMain, it
looks
> perfect. Can you post your code? I have also posted the code to get the
> bitmap bits and load them into an array:

> Dim bdSrc As Imaging.BitmapData

> Dim lArrayLength As Long

> Dim rTemp As New Rectangle(0, 0, 1, 1)

> Dim bSrc() As Byte

> rTemp.Size = bmpSrc.Size

> lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> '// resize the array to fit bits

> ReDim bSrc(lArrayLength)

> '// lock the memory

> bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite,
> bmpSrc.PixelFormat)

> '// copy to managed mem

> Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

> '// ...process bitarray...

> '// move back into unmanaged memory

> Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

> '// restore

> bmpSrc.UnlockBits(bdSrc)

> Jeremy



> > I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
> > start. Whilst programming in VB6 I never had the need to play around
with
> > Bitmap bits and so never learned anything about constructing bitmaps or
> > extracting information about their structure. Whenever I wanted to do
> > something like this I would simply BitBlt or TransparentBlt to the dc.
> Then
> > again I never had XP Icons to play with so that solution may not have
> worked
> > either.

> > If you find the project I would be interested in some code. I'll just go
> > through google and search for bitmap manipulation in VB\VC and see if
that
> > sheds any light.

> > The Image is drawn correctly if I use DrawIcon in a Picturebox's
Graphics
> > object, but not in a Bitmaps Graphics object. When I assign an XP Icon
to
> an
> > Image property at design time I have noticed that it's RawFormat is
> changed
> > to PNG. Maybe that's the answer, but I think that is just the default
> format
> > for a new bitmap. The other obvious difference is that the image is
stored
> > in the resx file, but I don't know how this is going to help me.

> > Thanks for any help.

> > Mick



> > >Have you actually checked the Bitmap bits to see if the alpha channel
> > exists? Should be able to load the bits after calling LockBitmapBits.
Just
> > reply if you want >some code on how to do this (it will take some
> searching
> > to find the project or I would post it now).

> > >It the Alpha channel exists, then you know the problem is in the
display
> > device.

> > >Jeremy



> > >> Does anybody know how to properly convert an XP Icon to bitmap with
all
> > the
> > >> Alpha Data intact.
> > >> If I use DrawIcon with the icon returned from ExtractIconEx the Icon
is
> > >> displayed as it should be.
> > >> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
> and
> > >> this Icon has shadows then the shadows are displayed as Dark (almost
> > black)
> > >> pixels.
> > >> What is more annoying is if I extract the Icon to a File I can then
use
> > >> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
> use:
> > >>     MyIcon = New Icon("MyFile.Ico")
> > >>     MyBitmap = Bitmap(MyIcon)

> > >> I've tried everything I can think of. I have even assigned the Icon
to
> an
> > >> Imagelist, there was a slight improvement there, but it was still not
> > right.

> > >> Q:    Why do I want to convert to a Bitmap?
> > >> A:    So I can assign it to an Image property in Code. If I assign an
> > Icon
> > >> to the Image Property at design time that works fine, but at runtime
I
> > >> cannot assign an Icon to a property expecting an Image, I have to use
> > >> Icon.ToBitmap which does not do the same thing.

> > >> Google turned up nothing of any use.

> > >> Use a Manifest --> Already using
> > >> Bitmap.MakeTransparent --> Does nothing.



Mon, 12 Sep 2005 02:46:14 GMT  
 ExtractIconEx
Mick,

The graphics object does support a full alpha channel, as does the Bitmap object. I have used them in the past to make a winamp-type visualization with tons of alpha blending. I know it works.

The problem is (i think) in the conversion from an Icon to a Bitmap, but I'm not sure where the bits are getting mangled. I have made a few test projects, and I will keep playing with it.

Make sure you post the solution if and when you find one (hopefully before i go insane).

:)

Jeremy

Quote:

> The problem is that the Graphics object when created from a bitmap does not
> fully support the Alpha channels of the Icon. A Picturebox does. It appears
> that the Alpha channel of the Memory Bitmap accepts On or Off. So the fully
> transparent pixels are not rendered and the semi transparent pixels are
> rendered as fully opaque.



> > I can draw the icon fine if I draw directly to a graphics object:

> Then what is the problem? Create a new bitmap, create a new graphics object
> from the bitmap, draw the icon on it using DrawIcon, then set the picture
> property = to the bitmap. However, I have been unsuccessful in getting the
> icon to draw properly using DrawIcon .

> Jeremy



> > The problem is that I want to set the icon to the Image property of a
> > dynamically created control, not draw it to a graphics object.
> > I can draw the icon fine if I draw directly to a graphics object:

> >  Dim g As Graphics = picMain.CreateGraphics
> >  g.DrawIcon(Me.Icon, 0, 0)
> >  g.Dispose()

> > or were you looking for the ExtractIconEx code:

> ////////////////////////////////////////////////////////////////////////////
> >   Imports Microsoft.Win32
> > ....
> > ....
> >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> >             (ByVal lpszFile As String, _
> >             ByVal nIconIndex As Integer, _
> >             ByVal phiconLarge As IntPtr(), _
> >             ByVal phiconSmall As IntPtr(), _
> >             ByVal nIcons As Integer) As Integer

> >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal Large As
> > Boolean = False) As Icon

> >         Dim hLarge() As IntPtr = {New IntPtr}
> >         Dim hSmall() As IntPtr = {New IntPtr}
> >         Dim ico As Icon

> >         'Some users customise their icons. So lets check the registry
> >         'for custom Icons first.
> >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _
> >                 ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> > Icons")

> >         'Return Shell32 and the Integer Passed in if the Key does not
> exist
> >         Dim s As String() = CType(rk.GetValue(n.ToString, "Shell32.dll," &
> > n), String).Split(",")

> >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> >         If Large = True Then
> >             ico = Icon.FromHandle(hLarge(0))
> >         Else
> >             ico = Icon.FromHandle(hSmall(0))
> >         End If

> >         'I think this does the same as DestroyIcon.
> >         hLarge(0) = IntPtr.Zero
> >         hSmall(0) = IntPtr.Zero

> >         Return ico

> >     End Function

> ////////////////////////////////////////////////////////////////////////////

> > I have found the following:

> > Assign the Icon to the Image property at Design Time ----> ImageFormat =
> > PNG----Looks Good

> > At runtime:
> > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> > Icon----Looks Good
> > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > MemoryBmp----Looks Very Bad

> > So it would seem that the key lies in ImageFormat but I don't seem to be
> > able to assign that, it's ReadOnly.

> > Now Why has OutLook stopped Indenting the Previous Message?

> > ------Previous

> Message---------------------------------------------------------------------
> > -------------------------



> > Well, I found the project, but I dont think thats the problem. The
> following
> > almost works fine for me:

> > '//////////////////////////////////////////
> > Dim bmpSource As Bitmap

> > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > '// From HICON

> > bmpSource = Bitmap.FromHicon(ico.Handle)

> > 'bmpSource = ico.ToBitmap

> > g.DrawImage(bmpSource, 0, 0)

> > g.Dispose()

> > '////////////////////////////////////////////

> > The only exception is that the icon looks a bit too small when I use the
> > graphics object to draw it. If I set the Image property of picMain, it
> looks
> > perfect. Can you post your code? I have also posted the code to get the
> > bitmap bits and load them into an array:

> > Dim bdSrc As Imaging.BitmapData

> > Dim lArrayLength As Long

> > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > Dim bSrc() As Byte

> > rTemp.Size = bmpSrc.Size

> > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > '// resize the array to fit bits

> > ReDim bSrc(lArrayLength)

> > '// lock the memory

> > bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite,
> > bmpSrc.PixelFormat)

> > '// copy to managed mem

> > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

> > '// ...process bitarray...

> > '// move back into unmanaged memory

> > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

> > '// restore

> > bmpSrc.UnlockBits(bdSrc)

> > Jeremy



> > > I haven't checked the Bitmap for Alpha channel. I wouldn't know where to
> > > start. Whilst programming in VB6 I never had the need to play around
> with
> > > Bitmap bits and so never learned anything about constructing bitmaps or
> > > extracting information about their structure. Whenever I wanted to do
> > > something like this I would simply BitBlt or TransparentBlt to the dc.
> > Then
> > > again I never had XP Icons to play with so that solution may not have
> > worked
> > > either.

> > > If you find the project I would be interested in some code. I'll just go
> > > through google and search for bitmap manipulation in VB\VC and see if
> that
> > > sheds any light.

> > > The Image is drawn correctly if I use DrawIcon in a Picturebox's
> Graphics
> > > object, but not in a Bitmaps Graphics object. When I assign an XP Icon
> to
> > an
> > > Image property at design time I have noticed that it's RawFormat is
> > changed
> > > to PNG. Maybe that's the answer, but I think that is just the default
> > format
> > > for a new bitmap. The other obvious difference is that the image is
> stored
> > > in the resx file, but I don't know how this is going to help me.

> > > Thanks for any help.

> > > Mick



> > > >Have you actually checked the Bitmap bits to see if the alpha channel
> > > exists? Should be able to load the bits after calling LockBitmapBits.
> Just
> > > reply if you want >some code on how to do this (it will take some
> > searching
> > > to find the project or I would post it now).

> > > >It the Alpha channel exists, then you know the problem is in the
> display
> > > device.

> > > >Jeremy



> > > >> Does anybody know how to properly convert an XP Icon to bitmap with
> all
> > > the
> > > >> Alpha Data intact.
> > > >> If I use DrawIcon with the icon returned from ExtractIconEx the Icon
> is
> > > >> displayed as it should be.
> > > >> If I convert this same Icon to a Bitmap with Bitmap(myIcon.ToBitmap)
> > and
> > > >> this Icon has shadows then the shadows are displayed as Dark (almost
> > > black)
> > > >> pixels.
> > > >> What is more annoying is if I extract the Icon to a File I can then
> use
> > > >> Bitmap("MyFile.ico") and this draws the icon correctly, but I cannot
> > use:
> > > >>     MyIcon = New Icon("MyFile.Ico")
> > > >>     MyBitmap = Bitmap(MyIcon)

> > > >> I've tried everything I can think of. I have even assigned the Icon
> to
> > an
> > > >> Imagelist, there was a slight improvement there, but it was still not
> > > right.

> > > >> Q:    Why do I want to convert to a Bitmap?
> > > >> A:    So I can assign it to an Image property in Code. If I assign an
> > > Icon
> > > >> to the Image Property at design time that works fine, but at runtime
> I
> > > >> cannot assign an Icon to a property expecting an Image, I have to use
> > > >> Icon.ToBitmap which does not do the same thing.

> > > >> Google turned up nothing of any use.

> > > >> Use a Manifest --> Already using
> > > >> Bitmap.MakeTransparent --> Does nothing.



Tue, 13 Sep 2005 06:14:46 GMT  
 ExtractIconEx
I have posted this question at windowsforms.net in the hope of getting an
answer there.
Browsing around I found a comment suggesting that we should use GetIconInfo
to get the hBitmaps and then get the DiBits from those and copy the DiBits
to the bitmap.
I haven't looked at that yet and won't be doing so over the weekend as
that's when I work. I need a rest anyway to let my hair grow back.
Sorry for getting you curious about this problem. It seems you have the same
pitfall as me and refuse to let go of something when a solution seems to be
so close.

If I get it working before you or anyone else posts a working solution I
will post it here.

Thanks
Mick
----------------------------------------------------------------------------
---------------------------


Mick,

The graphics object does support a full alpha channel, as does the Bitmap
object. I have used them in the past to make a winamp-type visualization
with tons of alpha blending. I know it works.

The problem is (i think) in the conversion from an Icon to a Bitmap, but I'm
not sure where the bits are getting mangled. I have made a few test
projects, and I will keep playing with it.

Make sure you post the solution if and when you find one (hopefully before i
go insane).

:)

Jeremy


Quote:
> The problem is that the Graphics object when created from a bitmap does
not
> fully support the Alpha channels of the Icon. A Picturebox does. It
appears
> that the Alpha channel of the Memory Bitmap accepts On or Off. So the
fully
> transparent pixels are not rendered and the semi transparent pixels are
> rendered as fully opaque.



> > I can draw the icon fine if I draw directly to a graphics object:

> Then what is the problem? Create a new bitmap, create a new graphics
object
> from the bitmap, draw the icon on it using DrawIcon, then set the picture
> property = to the bitmap. However, I have been unsuccessful in getting the
> icon to draw properly using DrawIcon .

> Jeremy



> > The problem is that I want to set the icon to the Image property of a
> > dynamically created control, not draw it to a graphics object.
> > I can draw the icon fine if I draw directly to a graphics object:

> >  Dim g As Graphics = picMain.CreateGraphics
> >  g.DrawIcon(Me.Icon, 0, 0)
> >  g.Dispose()

> > or were you looking for the ExtractIconEx code:

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:
> >   Imports Microsoft.Win32
> > ....
> > ....
> >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> >             (ByVal lpszFile As String, _
> >             ByVal nIconIndex As Integer, _
> >             ByVal phiconLarge As IntPtr(), _
> >             ByVal phiconSmall As IntPtr(), _
> >             ByVal nIcons As Integer) As Integer

> >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal Large
As
> > Boolean = False) As Icon

> >         Dim hLarge() As IntPtr = {New IntPtr}
> >         Dim hSmall() As IntPtr = {New IntPtr}
> >         Dim ico As Icon

> >         'Some users customise their icons. So lets check the registry
> >         'for custom Icons first.
> >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _

("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell

- Show quoted text -

Quote:
> > Icons")

> >         'Return Shell32 and the Integer Passed in if the Key does not
> exist
> >         Dim s As String() = CType(rk.GetValue(n.ToString, "Shell32.dll,"
&
> > n), String).Split(",")

> >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> >         If Large = True Then
> >             ico = Icon.FromHandle(hLarge(0))
> >         Else
> >             ico = Icon.FromHandle(hSmall(0))
> >         End If

> >         'I think this does the same as DestroyIcon.
> >         hLarge(0) = IntPtr.Zero
> >         hSmall(0) = IntPtr.Zero

> >         Return ico

> >     End Function

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:

> > I have found the following:

> > Assign the Icon to the Image property at Design Time ----> ImageFormat =
> > PNG----Looks Good

> > At runtime:
> > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> > Icon----Looks Good
> > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > MemoryBmp----Looks Very Bad

> > So it would seem that the key lies in ImageFormat but I don't seem to be
> > able to assign that, it's ReadOnly.

> > Now Why has OutLook stopped Indenting the Previous Message?

> > ------Previous

Message---------------------------------------------------------------------

- Show quoted text -

Quote:
> > -------------------------



> > Well, I found the project, but I dont think thats the problem. The
> following
> > almost works fine for me:

> > '//////////////////////////////////////////
> > Dim bmpSource As Bitmap

> > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > '// From HICON

> > bmpSource = Bitmap.FromHicon(ico.Handle)

> > 'bmpSource = ico.ToBitmap

> > g.DrawImage(bmpSource, 0, 0)

> > g.Dispose()

> > '////////////////////////////////////////////

> > The only exception is that the icon looks a bit too small when I use the
> > graphics object to draw it. If I set the Image property of picMain, it
> looks
> > perfect. Can you post your code? I have also posted the code to get the
> > bitmap bits and load them into an array:

> > Dim bdSrc As Imaging.BitmapData

> > Dim lArrayLength As Long

> > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > Dim bSrc() As Byte

> > rTemp.Size = bmpSrc.Size

> > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > '// resize the array to fit bits

> > ReDim bSrc(lArrayLength)

> > '// lock the memory

> > bdSrc = bmpSrc.LockBits(rTemp, Drawing.Imaging.ImageLockMode.ReadWrite,
> > bmpSrc.PixelFormat)

> > '// copy to managed mem

> > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0, lArrayLength)

> > '// ...process bitarray...

> > '// move back into unmanaged memory

> > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0, lArrayLength)

> > '// restore

> > bmpSrc.UnlockBits(bdSrc)

> > Jeremy



> > > I haven't checked the Bitmap for Alpha channel. I wouldn't know where
to
> > > start. Whilst programming in VB6 I never had the need to play around
> with
> > > Bitmap bits and so never learned anything about constructing bitmaps
or
> > > extracting information about their structure. Whenever I wanted to do
> > > something like this I would simply BitBlt or TransparentBlt to the dc.
> > Then
> > > again I never had XP Icons to play with so that solution may not have
> > worked
> > > either.

> > > If you find the project I would be interested in some code. I'll just
go
> > > through google and search for bitmap manipulation in VB\VC and see if
> that
> > > sheds any light.

> > > The Image is drawn correctly if I use DrawIcon in a Picturebox's
> Graphics
> > > object, but not in a Bitmaps Graphics object. When I assign an XP Icon
> to
> > an
> > > Image property at design time I have noticed that it's RawFormat is
> > changed
> > > to PNG. Maybe that's the answer, but I think that is just the default
> > format
> > > for a new bitmap. The other obvious difference is that the image is
> stored
> > > in the resx file, but I don't know how this is going to help me.

> > > Thanks for any help.

> > > Mick



> > > >Have you actually checked the Bitmap bits to see if the alpha channel
> > > exists? Should be able to load the bits after calling LockBitmapBits.
> Just
> > > reply if you want >some code on how to do this (it will take some
> > searching
> > > to find the project or I would post it now).

> > > >It the Alpha channel exists, then you know the problem is in the
> display
> > > device.

> > > >Jeremy



> > > >> Does anybody know how to properly convert an XP Icon to bitmap with
> all
> > > the
> > > >> Alpha Data intact.
> > > >> If I use DrawIcon with the icon returned from ExtractIconEx the
Icon
> is
> > > >> displayed as it should be.
> > > >> If I convert this same Icon to a Bitmap with

Bitmap(myIcon.ToBitmap)

- Show quoted text -

Quote:
> > and
> > > >> this Icon has shadows then the shadows are displayed as Dark
(almost
> > > black)
> > > >> pixels.
> > > >> What is more annoying is if I extract the Icon to a File I can then
> use
> > > >> Bitmap("MyFile.ico") and this draws the icon correctly, but I
cannot
> > use:
> > > >>     MyIcon = New Icon("MyFile.Ico")
> > > >>     MyBitmap = Bitmap(MyIcon)

> > > >> I've tried everything I can think of. I have even assigned the Icon
> to
> > an
> > > >> Imagelist, there was a slight improvement there, but it was still
not
> > > right.

> > > >> Q:    Why do I want to convert to a Bitmap?
> > > >> A:    So I can assign it to an Image property in

...

read more »



Tue, 13 Sep 2005 06:51:53 GMT  
 ExtractIconEx
I give up :-(

GetIconInfo returns two hbitmaps. One of them a monochrome mask, the other a
32bppRGB color image. I can't get it to return a 32bppARGB color image.


Quote:
> I have posted this question at windowsforms.net in the hope of getting an
> answer there.
> Browsing around I found a comment suggesting that we should use
GetIconInfo
> to get the hBitmaps and then get the DiBits from those and copy the DiBits
> to the bitmap.
> I haven't looked at that yet and won't be doing so over the weekend as
> that's when I work. I need a rest anyway to let my hair grow back.
> Sorry for getting you curious about this problem. It seems you have the
same
> pitfall as me and refuse to let go of something when a solution seems to
be
> so close.

> If I get it working before you or anyone else posts a working solution I
> will post it here.

> Thanks
> Mick
> --------------------------------------------------------------------------
--
> ---------------------------



> Mick,

> The graphics object does support a full alpha channel, as does the Bitmap
> object. I have used them in the past to make a winamp-type visualization
> with tons of alpha blending. I know it works.

> The problem is (i think) in the conversion from an Icon to a Bitmap, but
I'm
> not sure where the bits are getting mangled. I have made a few test
> projects, and I will keep playing with it.

> Make sure you post the solution if and when you find one (hopefully before
i
> go insane).

> :)

> Jeremy



> > The problem is that the Graphics object when created from a bitmap does
> not
> > fully support the Alpha channels of the Icon. A Picturebox does. It
> appears
> > that the Alpha channel of the Memory Bitmap accepts On or Off. So the
> fully
> > transparent pixels are not rendered and the semi transparent pixels are
> > rendered as fully opaque.



> > > I can draw the icon fine if I draw directly to a graphics object:

> > Then what is the problem? Create a new bitmap, create a new graphics
> object
> > from the bitmap, draw the icon on it using DrawIcon, then set the
picture
> > property = to the bitmap. However, I have been unsuccessful in getting
the
> > icon to draw properly using DrawIcon .

> > Jeremy



> > > The problem is that I want to set the icon to the Image property of a
> > > dynamically created control, not draw it to a graphics object.
> > > I can draw the icon fine if I draw directly to a graphics object:

> > >  Dim g As Graphics = picMain.CreateGraphics
> > >  g.DrawIcon(Me.Icon, 0, 0)
> > >  g.Dispose()

> > > or were you looking for the ExtractIconEx code:

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:
> > >   Imports Microsoft.Win32
> > > ....
> > > ....
> > >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> > >             (ByVal lpszFile As String, _
> > >             ByVal nIconIndex As Integer, _
> > >             ByVal phiconLarge As IntPtr(), _
> > >             ByVal phiconSmall As IntPtr(), _
> > >             ByVal nIcons As Integer) As Integer

> > >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal
Large
> As
> > > Boolean = False) As Icon

> > >         Dim hLarge() As IntPtr = {New IntPtr}
> > >         Dim hSmall() As IntPtr = {New IntPtr}
> > >         Dim ico As Icon

> > >         'Some users customise their icons. So lets check the registry
> > >         'for custom Icons first.
> > >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _

> ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> > > Icons")

> > >         'Return Shell32 and the Integer Passed in if the Key does not
> > exist
> > >         Dim s As String() = CType(rk.GetValue(n.ToString,
"Shell32.dll,"
> &
> > > n), String).Split(",")

> > >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> > >         If Large = True Then
> > >             ico = Icon.FromHandle(hLarge(0))
> > >         Else
> > >             ico = Icon.FromHandle(hSmall(0))
> > >         End If

> > >         'I think this does the same as DestroyIcon.
> > >         hLarge(0) = IntPtr.Zero
> > >         hSmall(0) = IntPtr.Zero

> > >         Return ico

> > >     End Function

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:

> > > I have found the following:

> > > Assign the Icon to the Image property at Design Time ----> ImageFormat
=
> > > PNG----Looks Good

> > > At runtime:
> > > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> > > Icon----Looks Good
> > > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > > MemoryBmp----Looks Very Bad

> > > So it would seem that the key lies in ImageFormat but I don't seem to
be
> > > able to assign that, it's ReadOnly.

> > > Now Why has OutLook stopped Indenting the Previous Message?

> > > ------Previous

Message---------------------------------------------------------------------

- Show quoted text -

Quote:
> > > -------------------------



> > > Well, I found the project, but I dont think thats the problem. The
> > following
> > > almost works fine for me:

> > > '//////////////////////////////////////////
> > > Dim bmpSource As Bitmap

> > > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > > '// From HICON

> > > bmpSource = Bitmap.FromHicon(ico.Handle)

> > > 'bmpSource = ico.ToBitmap

> > > g.DrawImage(bmpSource, 0, 0)

> > > g.Dispose()

> > > '////////////////////////////////////////////

> > > The only exception is that the icon looks a bit too small when I use
the
> > > graphics object to draw it. If I set the Image property of picMain, it
> > looks
> > > perfect. Can you post your code? I have also posted the code to get
the
> > > bitmap bits and load them into an array:

> > > Dim bdSrc As Imaging.BitmapData

> > > Dim lArrayLength As Long

> > > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > > Dim bSrc() As Byte

> > > rTemp.Size = bmpSrc.Size

> > > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > > '// resize the array to fit bits

> > > ReDim bSrc(lArrayLength)

> > > '// lock the memory

> > > bdSrc = bmpSrc.LockBits(rTemp,

Drawing.Imaging.ImageLockMode.ReadWrite,

- Show quoted text -

Quote:
> > > bmpSrc.PixelFormat)

> > > '// copy to managed mem

> > > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0,
lArrayLength)

> > > '// ...process bitarray...

> > > '// move back into unmanaged memory

> > > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0,
lArrayLength)

> > > '// restore

> > > bmpSrc.UnlockBits(bdSrc)

> > > Jeremy



> > > > I haven't checked the Bitmap for Alpha channel. I wouldn't know
where
> to
> > > > start. Whilst programming in VB6 I never had the need to play around
> > with
> > > > Bitmap bits and so never learned anything about constructing bitmaps
> or
> > > > extracting information about their structure. Whenever I wanted to
do
> > > > something like this I would simply BitBlt or TransparentBlt to the
dc.
> > > Then
> > > > again I never had XP Icons to play with so that solution may not
have
> > > worked
> > > > either.

> > > > If you find the project I would be interested in some code. I'll
just
> go
> > > > through google and search for bitmap manipulation in VB\VC and see
if
> > that
> > > > sheds any light.

> > > > The Image is drawn correctly if I use DrawIcon in a Picturebox's
> > Graphics
> > > > object, but not in a Bitmaps Graphics object. When I assign an XP
Icon
> > to
> > > an
> > > > Image property at design time I have noticed that it's RawFormat is
> > > changed
> > > > to PNG. Maybe that's the answer, but I think that is just the
default
> > > format
> > > > for a new bitmap. The other obvious difference is that the image is
> > stored
> > > > in the resx file, but I don't know how this is going to help me.

> > > > Thanks for any help.

> > > > Mick



> > > > >Have you actually checked the Bitmap bits to see if the alpha
channel
> > > > exists? Should be able to load the bits after calling
LockBitmapBits.
> > Just
> > > > reply if you want >some code on how to do this (it will take some
> > > searching
> > > > to find the project or I would post it now).

> > > > >It the Alpha channel exists, then you know the problem is in the
> > display
> > > > device.

> > > > >Jeremy



> > > > >> Does anybody know how to properly convert an XP Icon to bitmap
with
> > all
> > > > the
> > > > >> Alpha Data intact.
> > > > >> If I use DrawIcon with

...

read more »



Sat, 17 Sep 2005 01:46:40 GMT  
 ExtractIconEx
I dont think ExtractIconEx is the right function to be using. The icons are stored internally in several different formats (8bit, 24+1, 24+8 (AntiAliased)) as well as sizes (16x16, 24x24, 32x32, 48x48, 96x96).


Also, this:

myIntPtr = IntPtr.Zero

is NOT the same as this:

DestroyIcon(myIntPtr)

You can also call Icon.Dispose( ) if you have created an Icon object from the hIcon.

Still looking... Jeremy

Quote:

> I give up :-(

> GetIconInfo returns two hbitmaps. One of them a monochrome mask, the other a
> 32bppRGB color image. I can't get it to return a 32bppARGB color image.



> > I have posted this question at windowsforms.net in the hope of getting an
> > answer there.
> > Browsing around I found a comment suggesting that we should use
> GetIconInfo
> > to get the hBitmaps and then get the DiBits from those and copy the DiBits
> > to the bitmap.
> > I haven't looked at that yet and won't be doing so over the weekend as
> > that's when I work. I need a rest anyway to let my hair grow back.
> > Sorry for getting you curious about this problem. It seems you have the
> same
> > pitfall as me and refuse to let go of something when a solution seems to
> be
> > so close.

> > If I get it working before you or anyone else posts a working solution I
> > will post it here.

> > Thanks
> > Mick
> > --------------------------------------------------------------------------
> --
> > ---------------------------



> > Mick,

> > The graphics object does support a full alpha channel, as does the Bitmap
> > object. I have used them in the past to make a winamp-type visualization
> > with tons of alpha blending. I know it works.

> > The problem is (i think) in the conversion from an Icon to a Bitmap, but
> I'm
> > not sure where the bits are getting mangled. I have made a few test
> > projects, and I will keep playing with it.

> > Make sure you post the solution if and when you find one (hopefully before
> i
> > go insane).

> > :)

> > Jeremy



> > > The problem is that the Graphics object when created from a bitmap does
> > not
> > > fully support the Alpha channels of the Icon. A Picturebox does. It
> > appears
> > > that the Alpha channel of the Memory Bitmap accepts On or Off. So the
> > fully
> > > transparent pixels are not rendered and the semi transparent pixels are
> > > rendered as fully opaque.



> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > Then what is the problem? Create a new bitmap, create a new graphics
> > object
> > > from the bitmap, draw the icon on it using DrawIcon, then set the
> picture
> > > property = to the bitmap. However, I have been unsuccessful in getting
> the
> > > icon to draw properly using DrawIcon .

> > > Jeremy



> > > > The problem is that I want to set the icon to the Image property of a
> > > > dynamically created control, not draw it to a graphics object.
> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > >  Dim g As Graphics = picMain.CreateGraphics
> > > >  g.DrawIcon(Me.Icon, 0, 0)
> > > >  g.Dispose()

> > > > or were you looking for the ExtractIconEx code:

> ////////////////////////////////////////////////////////////////////////////
> > > >   Imports Microsoft.Win32
> > > > ....
> > > > ....
> > > >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> > > >             (ByVal lpszFile As String, _
> > > >             ByVal nIconIndex As Integer, _
> > > >             ByVal phiconLarge As IntPtr(), _
> > > >             ByVal phiconSmall As IntPtr(), _
> > > >             ByVal nIcons As Integer) As Integer

> > > >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal
> Large
> > As
> > > > Boolean = False) As Icon

> > > >         Dim hLarge() As IntPtr = {New IntPtr}
> > > >         Dim hSmall() As IntPtr = {New IntPtr}
> > > >         Dim ico As Icon

> > > >         'Some users customise their icons. So lets check the registry
> > > >         'for custom Icons first.
> > > >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _

> > ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> > > > Icons")

> > > >         'Return Shell32 and the Integer Passed in if the Key does not
> > > exist
> > > >         Dim s As String() = CType(rk.GetValue(n.ToString,
> "Shell32.dll,"
> > &
> > > > n), String).Split(",")

> > > >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> > > >         If Large = True Then
> > > >             ico = Icon.FromHandle(hLarge(0))
> > > >         Else
> > > >             ico = Icon.FromHandle(hSmall(0))
> > > >         End If

> > > >         'I think this does the same as DestroyIcon.
> > > >         hLarge(0) = IntPtr.Zero
> > > >         hSmall(0) = IntPtr.Zero

> > > >         Return ico

> > > >     End Function

> ////////////////////////////////////////////////////////////////////////////

> > > > I have found the following:

> > > > Assign the Icon to the Image property at Design Time ----> ImageFormat
> =
> > > > PNG----Looks Good

> > > > At runtime:
> > > > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> > > > Icon----Looks Good
> > > > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > > > MemoryBmp----Looks Very Bad

> > > > So it would seem that the key lies in ImageFormat but I don't seem to
> be
> > > > able to assign that, it's ReadOnly.

> > > > Now Why has OutLook stopped Indenting the Previous Message?

> > > > ------Previous

> Message---------------------------------------------------------------------
> > > > -------------------------



> > > > Well, I found the project, but I dont think thats the problem. The
> > > following
> > > > almost works fine for me:

> > > > '//////////////////////////////////////////
> > > > Dim bmpSource As Bitmap

> > > > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > > > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > > > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > > > '// From HICON

> > > > bmpSource = Bitmap.FromHicon(ico.Handle)

> > > > 'bmpSource = ico.ToBitmap

> > > > g.DrawImage(bmpSource, 0, 0)

> > > > g.Dispose()

> > > > '////////////////////////////////////////////

> > > > The only exception is that the icon looks a bit too small when I use
> the
> > > > graphics object to draw it. If I set the Image property of picMain, it
> > > looks
> > > > perfect. Can you post your code? I have also posted the code to get
> the
> > > > bitmap bits and load them into an array:

> > > > Dim bdSrc As Imaging.BitmapData

> > > > Dim lArrayLength As Long

> > > > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > > > Dim bSrc() As Byte

> > > > rTemp.Size = bmpSrc.Size

> > > > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > > > '// resize the array to fit bits

> > > > ReDim bSrc(lArrayLength)

> > > > '// lock the memory

> > > > bdSrc = bmpSrc.LockBits(rTemp,
> Drawing.Imaging.ImageLockMode.ReadWrite,
> > > > bmpSrc.PixelFormat)

> > > > '// copy to managed mem

> > > > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0,
> lArrayLength)

> > > > '// ...process bitarray...

> > > > '// move back into unmanaged memory

> > > > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0,
> lArrayLength)

> > > > '// restore

> > > > bmpSrc.UnlockBits(bdSrc)

> > > > Jeremy



> > > > > I haven't checked the Bitmap for Alpha channel. I wouldn't know
> where
> > to
> > > > > start. Whilst programming in VB6 I never had the need to play around
> > > with
> > > > > Bitmap bits and so never learned anything about constructing bitmaps
> > or
> > > > > extracting information about their structure. Whenever I wanted to
> do
> > > > > something like this I would simply BitBlt or TransparentBlt to the
> dc.
> > > > Then
> > > > > again I never had XP Icons to play with so that solution may not
> have
> > > > worked
> > > > > either.

> > > > > If you find the project I would be interested in some code. I'll
> just
> > go
> > > > > through google and search for bitmap manipulation in VB\VC and see
> if
> > > that
> > > > > sheds any light.

> > > > > The Image is drawn correctly if I use DrawIcon in a Picturebox's
> > > Graphics
> > > > > object, but not in a Bitmaps Graphics

...

read more »



Sat, 17 Sep 2005 04:18:43 GMT  
 ExtractIconEx
Ok, this was wrong:

Quote:

>>a function to get the icon in the proper format. I have looked through the icon functions and i havent found one yet
>> (except maybe ExtractAssociatedIcon).

We are getting the 32 bpp icon... still dont understand why it's not displaying right though.

I dont think ExtractIconEx is the right function to be using. The icons are stored internally in several different formats (8bit, 24+1, 24+8 (AntiAliased)) as well as sizes (16x16, 24x24, 32x32, 48x48, 96x96).


Also, this:

myIntPtr = IntPtr.Zero

is NOT the same as this:

DestroyIcon(myIntPtr)

You can also call Icon.Dispose( ) if you have created an Icon object from the hIcon.

Still looking... Jeremy

Quote:

> I give up :-(

> GetIconInfo returns two hbitmaps. One of them a monochrome mask, the other a
> 32bppRGB color image. I can't get it to return a 32bppARGB color image.



> > I have posted this question at windowsforms.net in the hope of getting an
> > answer there.
> > Browsing around I found a comment suggesting that we should use
> GetIconInfo
> > to get the hBitmaps and then get the DiBits from those and copy the DiBits
> > to the bitmap.
> > I haven't looked at that yet and won't be doing so over the weekend as
> > that's when I work. I need a rest anyway to let my hair grow back.
> > Sorry for getting you curious about this problem. It seems you have the
> same
> > pitfall as me and refuse to let go of something when a solution seems to
> be
> > so close.

> > If I get it working before you or anyone else posts a working solution I
> > will post it here.

> > Thanks
> > Mick
> > --------------------------------------------------------------------------
> --
> > ---------------------------



> > Mick,

> > The graphics object does support a full alpha channel, as does the Bitmap
> > object. I have used them in the past to make a winamp-type visualization
> > with tons of alpha blending. I know it works.

> > The problem is (i think) in the conversion from an Icon to a Bitmap, but
> I'm
> > not sure where the bits are getting mangled. I have made a few test
> > projects, and I will keep playing with it.

> > Make sure you post the solution if and when you find one (hopefully before
> i
> > go insane).

> > :)

> > Jeremy



> > > The problem is that the Graphics object when created from a bitmap does
> > not
> > > fully support the Alpha channels of the Icon. A Picturebox does. It
> > appears
> > > that the Alpha channel of the Memory Bitmap accepts On or Off. So the
> > fully
> > > transparent pixels are not rendered and the semi transparent pixels are
> > > rendered as fully opaque.



> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > Then what is the problem? Create a new bitmap, create a new graphics
> > object
> > > from the bitmap, draw the icon on it using DrawIcon, then set the
> picture
> > > property = to the bitmap. However, I have been unsuccessful in getting
> the
> > > icon to draw properly using DrawIcon .

> > > Jeremy



> > > > The problem is that I want to set the icon to the Image property of a
> > > > dynamically created control, not draw it to a graphics object.
> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > >  Dim g As Graphics = picMain.CreateGraphics
> > > >  g.DrawIcon(Me.Icon, 0, 0)
> > > >  g.Dispose()

> > > > or were you looking for the ExtractIconEx code:

> ////////////////////////////////////////////////////////////////////////////
> > > >   Imports Microsoft.Win32
> > > > ....
> > > > ....
> > > >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> > > >             (ByVal lpszFile As String, _
> > > >             ByVal nIconIndex As Integer, _
> > > >             ByVal phiconLarge As IntPtr(), _
> > > >             ByVal phiconSmall As IntPtr(), _
> > > >             ByVal nIcons As Integer) As Integer

> > > >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal
> Large
> > As
> > > > Boolean = False) As Icon

> > > >         Dim hLarge() As IntPtr = {New IntPtr}
> > > >         Dim hSmall() As IntPtr = {New IntPtr}
> > > >         Dim ico As Icon

> > > >         'Some users customise their icons. So lets check the registry
> > > >         'for custom Icons first.
> > > >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _

> > ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> > > > Icons")

> > > >         'Return Shell32 and the Integer Passed in if the Key does not
> > > exist
> > > >         Dim s As String() = CType(rk.GetValue(n.ToString,
> "Shell32.dll,"
> > &
> > > > n), String).Split(",")

> > > >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> > > >         If Large = True Then
> > > >             ico = Icon.FromHandle(hLarge(0))
> > > >         Else
> > > >             ico = Icon.FromHandle(hSmall(0))
> > > >         End If

> > > >         'I think this does the same as DestroyIcon.
> > > >         hLarge(0) = IntPtr.Zero
> > > >         hSmall(0) = IntPtr.Zero

> > > >         Return ico

> > > >     End Function

> ////////////////////////////////////////////////////////////////////////////

> > > > I have found the following:

> > > > Assign the Icon to the Image property at Design Time ----> ImageFormat
> =
> > > > PNG----Looks Good

> > > > At runtime:
> > > > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat =
> > > > Icon----Looks Good
> > > > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > > > MemoryBmp----Looks Very Bad

> > > > So it would seem that the key lies in ImageFormat but I don't seem to
> be
> > > > able to assign that, it's ReadOnly.

> > > > Now Why has OutLook stopped Indenting the Previous Message?

> > > > ------Previous

> Message---------------------------------------------------------------------
> > > > -------------------------



> > > > Well, I found the project, but I dont think thats the problem. The
> > > following
> > > > almost works fine for me:

> > > > '//////////////////////////////////////////
> > > > Dim bmpSource As Bitmap

> > > > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > > > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > > > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > > > '// From HICON

> > > > bmpSource = Bitmap.FromHicon(ico.Handle)

> > > > 'bmpSource = ico.ToBitmap

> > > > g.DrawImage(bmpSource, 0, 0)

> > > > g.Dispose()

> > > > '////////////////////////////////////////////

> > > > The only exception is that the icon looks a bit too small when I use
> the
> > > > graphics object to draw it. If I set the Image property of picMain, it
> > > looks
> > > > perfect. Can you post your code? I have also posted the code to get
> the
> > > > bitmap bits and load them into an array:

> > > > Dim bdSrc As Imaging.BitmapData

> > > > Dim lArrayLength As Long

> > > > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > > > Dim bSrc() As Byte

> > > > rTemp.Size = bmpSrc.Size

> > > > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > > > '// resize the array to fit bits

> > > > ReDim bSrc(lArrayLength)

> > > > '// lock the memory

> > > > bdSrc = bmpSrc.LockBits(rTemp,
> Drawing.Imaging.ImageLockMode.ReadWrite,
> > > > bmpSrc.PixelFormat)

> > > > '// copy to managed mem

> > > > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0,
> lArrayLength)

> > > > '// ...process bitarray...

> > > > '// move back into unmanaged memory

> > > > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0,
> lArrayLength)

> > > > '// restore

> > > > bmpSrc.UnlockBits(bdSrc)

> > > > Jeremy



> > > > > I haven't checked the Bitmap for Alpha channel. I wouldn't know
> where
> > to
> > > > > start. Whilst programming in VB6 I never had the need to play around
> > > with
> > > > > Bitmap bits and so never learned anything about constructing bitmaps
> > or
> > > > > extracting information about their structure. Whenever I wanted to
> do
> > > > > something like this I would simply BitBlt or TransparentBlt to

...

read more »



Sat, 17 Sep 2005 04:37:23 GMT  
 ExtractIconEx
I am almost positive that it's a problem with the Memory Bitmap defaulting
to 24+1, or less and nothing at all to do with the Image conversions.
I suppose we could use LoadLibrary, FindResource and LoadResource to extract
the desired Icon instead of ExtractIconEx.


Ok, this was wrong:

({*filter*} black border). So there must be

Quote:
>>a function to get the icon in the proper format. I have looked through the

icon functions and i havent found one yet

Quote:
>> (except maybe ExtractAssociatedIcon).

We are getting the 32 bpp icon... still dont understand why it's not
displaying right though.


I dont think ExtractIconEx is the right function to be using. The icons are
stored internally in several different formats (8bit, 24+1, 24+8
(AntiAliased)) as well as sizes (16x16, 24x24, 32x32, 48x48, 96x96).


({*filter*} black border). So there must be a function to get the icon in the
proper format. I have looked through the icon functions and i havent found
one yet (except maybe ExtractAssociatedIcon).

Also, this:

myIntPtr = IntPtr.Zero

is NOT the same as this:

DestroyIcon(myIntPtr)

You can also call Icon.Dispose( ) if you have created an Icon object from
the hIcon.

Still looking... Jeremy


Quote:
> I give up :-(

> GetIconInfo returns two hbitmaps. One of them a monochrome mask, the other
a
> 32bppRGB color image. I can't get it to return a 32bppARGB color image.



> > I have posted this question at windowsforms.net in the hope of getting
an
> > answer there.
> > Browsing around I found a comment suggesting that we should use
> GetIconInfo
> > to get the hBitmaps and then get the DiBits from those and copy the
DiBits
> > to the bitmap.
> > I haven't looked at that yet and won't be doing so over the weekend as
> > that's when I work. I need a rest anyway to let my hair grow back.
> > Sorry for getting you curious about this problem. It seems you have the
> same
> > pitfall as me and refuse to let go of something when a solution seems to
> be
> > so close.

> > If I get it working before you or anyone else posts a working solution I
> > will post it here.

> > Thanks
> > Mick

> --------------------------------------------------------------------------
> --
> > ---------------------------



> > Mick,

> > The graphics object does support a full alpha channel, as does the
Bitmap
> > object. I have used them in the past to make a winamp-type visualization
> > with tons of alpha blending. I know it works.

> > The problem is (i think) in the conversion from an Icon to a Bitmap, but
> I'm
> > not sure where the bits are getting mangled. I have made a few test
> > projects, and I will keep playing with it.

> > Make sure you post the solution if and when you find one (hopefully
before
> i
> > go insane).

> > :)

> > Jeremy



> > > The problem is that the Graphics object when created from a bitmap
does
> > not
> > > fully support the Alpha channels of the Icon. A Picturebox does. It
> > appears
> > > that the Alpha channel of the Memory Bitmap accepts On or Off. So the
> > fully
> > > transparent pixels are not rendered and the semi transparent pixels
are
> > > rendered as fully opaque.



> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > Then what is the problem? Create a new bitmap, create a new graphics
> > object
> > > from the bitmap, draw the icon on it using DrawIcon, then set the
> picture
> > > property = to the bitmap. However, I have been unsuccessful in getting
> the
> > > icon to draw properly using DrawIcon .

> > > Jeremy



> > > > The problem is that I want to set the icon to the Image property of
a
> > > > dynamically created control, not draw it to a graphics object.
> > > > I can draw the icon fine if I draw directly to a graphics object:

> > > >  Dim g As Graphics = picMain.CreateGraphics
> > > >  g.DrawIcon(Me.Icon, 0, 0)
> > > >  g.Dispose()

> > > > or were you looking for the ExtractIconEx code:

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:
> > > >   Imports Microsoft.Win32
> > > > ....
> > > > ....
> > > >     Private Declare Auto Function ExtractIconEx Lib "shell32.dll" _
> > > >             (ByVal lpszFile As String, _
> > > >             ByVal nIconIndex As Integer, _
> > > >             ByVal phiconLarge As IntPtr(), _
> > > >             ByVal phiconSmall As IntPtr(), _
> > > >             ByVal nIcons As Integer) As Integer

> > > >     Private Function ShellIcon(ByVal n As Integer, Optional ByVal
> Large
> > As
> > > > Boolean = False) As Icon

> > > >         Dim hLarge() As IntPtr = {New IntPtr}
> > > >         Dim hSmall() As IntPtr = {New IntPtr}
> > > >         Dim ico As Icon

> > > >         'Some users customise their icons. So lets check the
registry
> > > >         'for custom Icons first.
> > > >         Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey _

> > ("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
> > > > Icons")

> > > >         'Return Shell32 and the Integer Passed in if the Key does
not
> > > exist
> > > >         Dim s As String() = CType(rk.GetValue(n.ToString,
> "Shell32.dll,"
> > &
> > > > n), String).Split(",")

> > > >         Call ExtractIconEx(s(0), CInt(s(1)), hLarge, hSmall, 1)

> > > >         If Large = True Then
> > > >             ico = Icon.FromHandle(hLarge(0))
> > > >         Else
> > > >             ico = Icon.FromHandle(hSmall(0))
> > > >         End If

> > > >         'I think this does the same as DestroyIcon.
> > > >         hLarge(0) = IntPtr.Zero
> > > >         hSmall(0) = IntPtr.Zero

> > > >         Return ico

> > > >     End Function

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:

> > > > I have found the following:

> > > > Assign the Icon to the Image property at Design Time ---->
ImageFormat
> =
> > > > PNG----Looks Good

> > > > At runtime:
> > > > bmp = New Bitmap("..\MyIcon.ico")   -------------------> ImageFormat
=
> > > > Icon----Looks Good
> > > > bmp = New Bitmap(ShellIcon(24).ToBitmap) -----------> ImageFormat =
> > > > MemoryBmp----Looks Very Bad

> > > > So it would seem that the key lies in ImageFormat but I don't seem
to
> be
> > > > able to assign that, it's ReadOnly.

> > > > Now Why has OutLook stopped Indenting the Previous Message?

> > > > ------Previous

Message---------------------------------------------------------------------

- Show quoted text -

Quote:
> > > > -------------------------



> > > > Well, I found the project, but I dont think thats the problem. The
> > > following
> > > > almost works fine for me:

> > > > '//////////////////////////////////////////
> > > > Dim bmpSource As Bitmap

> > > > Dim ico As New Icon("C:\Documents and Settings\Jeremy.ASI\My
> > > > Documents\Graphics\icons\Macromedia\Flash\union.ico")

> > > > Dim g As Graphics = Graphics.FromImage(Me.picMain.Image)

> > > > '// From HICON

> > > > bmpSource = Bitmap.FromHicon(ico.Handle)

> > > > 'bmpSource = ico.ToBitmap

> > > > g.DrawImage(bmpSource, 0, 0)

> > > > g.Dispose()

> > > > '////////////////////////////////////////////

> > > > The only exception is that the icon looks a bit too small when I use
> the
> > > > graphics object to draw it. If I set the Image property of picMain,
it
> > > looks
> > > > perfect. Can you post your code? I have also posted the code to get
> the
> > > > bitmap bits and load them into an array:

> > > > Dim bdSrc As Imaging.BitmapData

> > > > Dim lArrayLength As Long

> > > > Dim rTemp As New Rectangle(0, 0, 1, 1)

> > > > Dim bSrc() As Byte

> > > > rTemp.Size = bmpSrc.Size

> > > > lArrayLength = (bmpSrc.Width * bmpSrc.Height) * 4

> > > > '// resize the array to fit bits

> > > > ReDim bSrc(lArrayLength)

> > > > '// lock the memory

> > > > bdSrc = bmpSrc.LockBits(rTemp,
> Drawing.Imaging.ImageLockMode.ReadWrite,
> > > > bmpSrc.PixelFormat)

> > > > '// copy to managed mem

> > > > Runtime.InteropServices.Marshal.Copy(bdSrc.Scan0, bSrc, 0,
> lArrayLength)

> > > > '// ...process bitarray...

> > > > '// move back into unmanaged memory

> > > > Runtime.InteropServices.Marshal.Copy(bSrc, 0, bdSrc.Scan0,
> lArrayLength)

> > > > '// restore

> > > > bmpSrc.UnlockBits(bdSrc)

> > > > Jeremy



> > > > > I haven't checked the Bitmap for Alpha

...

read more »



Sat, 17 Sep 2005 08:46:47 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. ExtractIconEx

2. ExtractIconEx?

3. ExtractIconEx: Heavily Guarded Secret

4. ExtractIconEx API call (VB4/Win95)

5. Problem using ExtractIconEx with ListView

6. Problem using ExtractIconEx with ListView

7. How to use ExtractIconEx?

8. ExtractIconEx (UseOf VB4-32)

9. Need Code: ExtractIconEx API

10. Problem using ExtractIconEx with ListView

11. ExtractIconEx - Please help

12. Help on use of ExtractIconEx

 

 
Powered by phpBB® Forum Software