Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
GeneralRe: Smart Build Pin
Heath Stewart25-Mar-04 4:02
protectorHeath Stewart25-Mar-04 4:02 
GeneralDataGrid Control Question Pin
DougW4824-Mar-04 16:54
DougW4824-Mar-04 16:54 
GeneralRe: DataGrid Control Question Pin
ian mariano24-Mar-04 17:28
ian mariano24-Mar-04 17:28 
GeneralRe: DataGrid Control Question Pin
Heath Stewart24-Mar-04 17:31
protectorHeath Stewart24-Mar-04 17:31 
Questiongetting mouse position? Pin
azusakt24-Mar-04 16:29
azusakt24-Mar-04 16:29 
AnswerRe: getting mouse position? Pin
Heath Stewart24-Mar-04 17:24
protectorHeath Stewart24-Mar-04 17:24 
QuestionHow can I save an icon? Pin
Flack24-Mar-04 14:40
Flack24-Mar-04 14:40 
AnswerRe: How can I save an icon? Pin
Heath Stewart24-Mar-04 17:21
protectorHeath Stewart24-Mar-04 17:21 
A few things - don't use a StreamWriter, you may run into encoding problems. An icon is a binary file, so just use something simple like a FileStream.

Also, instead of Application.StartupPath + "/TestI.ico", use Path.Combine(Application.StartupPath, "TestI.ico"), which properly combines the directories and/or filename with the correct directory separator. It may have worked this time, but it usually won't depending on the circumstances.

Finally to the real problem. The icon support in the .NET stinks. Icon files contain a header section that describes the resolution and addresses of indexed bitmaps (has palettes) within the file. Because of an apparent but in the System.Drawing classes, the 32-bit icon (which contains the per-pixel alpha channel) is not well preserved (and sometimes not at all).

The best way is to P/Invoke the various icon functions from the Windows Shell APIs, like SHCreateFileExtractIcon, which uses the COM interface IExtractIcon, which is pretty simple to declare as a managed interface (be sure to use the GuidAttribute with the right interface ID (IID)).

As far as extracting various resolutions from an icon file with multiple bitmaps, this is exactly what Windows does with methods like that from above and all the lower-level icon functions like GetIcon. Most of the time, the small and large icon handles (HICON) are returned at once with the appropriate resolution. In order to get 32-bit icons with the native APIs, you must use an application manifest to bind to Common Controls 6, which is described in my article Windows XP Visual Styles for Windows Forms[^]. If you're using .NET 1.1 or higher, you can also call Application.EnableVisualStyles before Application.Run, but be sure to read the documentation for a few quirks (like calling Application.DoEvents in between the two to avoid common problems).

If you want complete control over the icons, how they save, and which icons are extract, read about Icons in Win32[^]. All those structs can be create in .NET easily enough, so long as you use the appropriate StructLayoutAttribute since some structs have a pack size, IIRC, of 2.

 

Microsoft MVP, Visual C#
My Articles
GeneralCapturing ListView scrollbar events Pin
chuck05324-Mar-04 10:45
chuck05324-Mar-04 10:45 
GeneralRe: Capturing ListView scrollbar events Pin
Heath Stewart24-Mar-04 11:24
protectorHeath Stewart24-Mar-04 11:24 
GeneralTray Icon Pin
RickZ24-Mar-04 10:34
RickZ24-Mar-04 10:34 
GeneralRe: Tray Icon Pin
Dave Kreskowiak24-Mar-04 11:08
mveDave Kreskowiak24-Mar-04 11:08 
GeneralRe: Tray Icon Pin
SapiensBwG26-Mar-04 5:41
SapiensBwG26-Mar-04 5:41 
GeneralAssembly - from Unmanaged Pin
Kant24-Mar-04 10:28
Kant24-Mar-04 10:28 
GeneralRe: Assembly - from Unmanaged Pin
Dave Kreskowiak24-Mar-04 10:33
mveDave Kreskowiak24-Mar-04 10:33 
GeneralRe: Assembly - from Unmanaged Pin
Kant24-Mar-04 11:07
Kant24-Mar-04 11:07 
GeneralRe: Assembly - from Unmanaged Pin
Heath Stewart24-Mar-04 11:22
protectorHeath Stewart24-Mar-04 11:22 
GeneralRe: Assembly - from Unmanaged Pin
Jeremy Kimball25-Mar-04 6:16
Jeremy Kimball25-Mar-04 6:16 
GeneralRe: Assembly - from Unmanaged Pin
Heath Stewart25-Mar-04 8:08
protectorHeath Stewart25-Mar-04 8:08 
GeneralRe: Assembly - from Unmanaged Pin
Jeremy Kimball25-Mar-04 9:05
Jeremy Kimball25-Mar-04 9:05 
GeneralRe: Assembly - from Unmanaged Pin
Heath Stewart25-Mar-04 9:35
protectorHeath Stewart25-Mar-04 9:35 
GeneralRe: Assembly - from Unmanaged Pin
Jeremy Kimball25-Mar-04 9:46
Jeremy Kimball25-Mar-04 9:46 
GeneralTo Heath (we talked about this subject before) Pin
profoundwhispers24-Mar-04 9:10
profoundwhispers24-Mar-04 9:10 
GeneralRe: To Heath (we talked about this subject before) Pin
Tom Larsen24-Mar-04 9:14
Tom Larsen24-Mar-04 9:14 
GeneralRe: To Heath (we talked about this subject before) Pin
profoundwhispers24-Mar-04 9:32
profoundwhispers24-Mar-04 9:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.