• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need some help in c#

kbirger

Member
First off, i'm writing a launcher program. It's going to have a context menu with all the apps that the user added (and their respective icons).

Considering that someone might put in 40 apps, that might take a while to cache EVERY icon EVERY time the program starts.

My solution was to add all the icons to an Arraylist and SERIALIZE the arraylist into a .dat file (just plaintext).

Problem: The icon that i add to the arraylist is 256/true-color with alpha transparency. The icon that comes out when i deserialize it is invariably 16 colors with no transparency/regular transparency.

i did some tests, this has nothing to do with the limitations of the icon class. (i displayed the before/after icon for myself). Also, i tested and it has nothing to do with how i'm storing the ArrayList of icons in a .dat file.

does anyone know what is going on or another way i could cache the icons without putting them into separate files?

note: it also seems to have something to do with the way i'm extracting the icon from the exes (SHFileInfo method imported from shell32.dll). If i extract an icon (16x16 16.8 mill color) from an exe and save it on disk, put it in an array, serialize it, unserialize it, check it. it seems to preserve the colors and the transparency, but not the alpha.
 
Make sure you're extracting the same number of bits from the file as what you're displaying..
 
the thing is i'm not doing any manual bit work.

i make a call that extracts the icon from the exe (i thoroughly tested this, it extracts the proper icon 100% perfectly)

i make another call that serializes the whole arraylist of icons into a stream and saves the stream to a file.

and a third call that reads the whole file into a stream, deserializing it
 
Assuming this is the 1.1 framework, are you calling Application.EnableVisualStyles() and Application.DoEvents() before Application.Run()?

There are several issues related to .NET and losing the alpha channel. Check the FAQ's here and see if any of those situations apply to you.

In particular, it's only the ImageList.Draw() method that can draw transparency. You should probably be adding your icons to an ImageList rather than an ArrayList and then using the ocrresponding Draw() method.
 
the thing is... if i use the icon that i havn't serialized yet, it works fine with the alpha and colors. (so it's not that some setting is preventing alpha from being disabled). and it's not just the alpha that's screwed up. the icon appears in 16 colors.

unless i'm not really understanding what you meant



2.17 Why does adding images to an ImageList in the designer cause them to lose their alpha channel?


Looks like the ImageList editor loses the transparency when it does some internal copy/clone of the images. However, it seems that it does work when you add the images in code to the ImageList.

One workaround (not so tidy) is to add the images to the ImageList in the design time (so that your design-time will be closer to the runtime) and then clear that ImageList and refill it with the images again, in code.

Take a look at this faq on how to add images to your project and retrive them programatically during runtime. Adding image files to a project as an embedded resource and retrieving them programatically.

i tried adding the icon after i deserialize it to an imagelist (didn't help)
 
I just did a quick little test, and this seems to be working for me. I serialized the icon as a png. Perhaps you are serializing as an icon. I don't believe .ico image formats support alpha transparency but png format definitely does.

This is just a little snippet. Obviously there are pieces missing, but pictureBox1 and pictureBox2 end up displaying exactly the same thing when I serialize as a png but NOT when I serialize as an icon.

Hope this helps.
 
And in case anyone was interested in the code I used to extract the icon from the exe, here it is
 
what's the Shell.GetFileIcon() method? because the methodi use to get the icon only gives me a handle for the icon ,then i have to get the icon from the handle...



API.SHGetFileInfoA(sFile, 0, ref SFInfo, Marshal.SizeOf(SFInfo), SHGFI.Icon |SHGFI.SmallIcon);
Icon ico = System.Drawing.Icon.FromHandle(SFInfo.hIcon);
 
Originally posted by: kbirger
what's the Shell.GetFileIcon() method? because the methodi use to get the icon only gives me a handle for the icon ,then i have to get the icon from the handle...



API.SHGetFileInfoA(sFile, 0, ref SFInfo, Marshal.SizeOf(SFInfo), SHGFI.Icon |SHGFI.SmallIcon);
Icon ico = System.Drawing.Icon.FromHandle(SFInfo.hIcon);

see my second post. I posted the Shell class I'm using.

 
heh, i'm sorry. i opened the thread, then left the room... then i read it... then i replied... missed the refresh..


but this should work if i make it add the image to an arraylist and serialize it?
 
Originally posted by: kbirger
heh, i'm sorry. i opened the thread, then left the room... then i read it... then i replied... missed the refresh..


but this should work if i make it add the image to an arraylist and serialize it?

It worked for me.

 
Thanks a lot for your help. i made some progress...
The alpha still doesn't work correctly, butthe colors do.

i used all your code except i had to separate the serialize and deserialize parts. the images come out as 16 color by default, alphablending doesn't work either way (although binary transparency works) and for some reason if i set the color depth to 32 (like in the pic above) the transparency gets a little weird.
 
Hmm.... you're right. I guess the icons I tested with didn't have a good alpha channel. Try this guy's code and see how that works. The only change I would make is that in framework v 1.1 you don't need a manifest file. Just call Application.EnableVisualStyles() and Application.DoEvents() before Application.Run().
 
sounds good i guess... looks like i have to try to avoid converting them to bitmaps though...

yeah, that just made me certain taht it's impossible to serialize the icons and maintain the alpha layer.
 
Back
Top