how to extract a specific icon from an .ico file?

  • Thread starter Thread starter Mehdi Mousavi
  • Start date Start date
M

Mehdi Mousavi

Hi,
I need to know how to extract a specific icon (32x32 for instance) from an
icon file that contains more than one icon size?

Any help would be highly appreciated,

Cheers.
Mehdi
 
Well, thanks. Would you please tell me how to do the same thing using the
resource files? I mean what if I store my icon in the resource file?

Thank you for your time,
Mehdi

Hiroaki SHIBUKI said:
Hi,
I need to know how to extract a specific icon (32x32 for instance) from an
icon file that contains more than one icon size?

Any help would be highly appreciated,

Try this ;-)

Icon icon1 = IconFactory (@"C:\hoge.ico", 32, 32);

--
Hiroaki SHIBUKI [MVP]

--IconFactory.cs: BEGIN--
using System;

namespace hidori.Drawing
{
using System.Runtime.InteropServices;
using System.Drawing;

public class IconFactory
{
internal enum IMAGE_TYPE : uint
{
IMAGE_BITMAP = 0,
IMAGE_ICON = 1,
IMAGE_CURSOR = 2,
IMAGE_ENHMETAFILE = 3,
}

internal enum LR_FLAGS : uint
{
LR_DEFAULTCOLOR = 0x0000,
LR_MONOCHROME = 0x0001,
LR_COLOR = 0x0002,
LR_COPYRETURNORG = 0x0004,
LR_COPYDELETEORG = 0x0008,
LR_LOADFROMFILE = 0x0010,
LR_LOADTRANSPARENT = 0x0020,
LR_DEFAULTSIZE = 0x0040,
LR_VGACOLOR = 0x0080,
LR_LOADMAP3DCOLORS = 0x1000,
LR_CREATEDIBSECTION = 0x2000,
LR_COPYFROMRESOURCE = 0x4000,
LR_SHARED = 0x8000,
}

[DllImport("user32.dll")]
internal static extern System.IntPtr LoadImage
(
System.IntPtr hinst,
string lpszName,
IMAGE_TYPE uType,
int cxDesired,
int cyDesired,
LR_FLAGS fuLoad
);

public static Icon FromFile(string sFileName)
{
System.IntPtr hIcon = LoadImage (System.IntPtr.Zero, sFileName,
IMAGE_TYPE.IMAGE_ICON, 0, 0,
LR_FLAGS.LR_LOADFROMFILE|LR_FLAGS.LR_DEFAULTSIZE);
return Icon.FromHandle (hIcon);
}

public static Icon FromFile(string sFileName, int width, int height)
{
System.IntPtr hIcon = LoadImage (System.IntPtr.Zero, sFileName,
IMAGE_TYPE.IMAGE_ICON, width, height, LR_FLAGS.LR_LOADFROMFILE);
return Icon.FromHandle (hIcon);
}

public static Icon FromFile(string sFileName, Size size)
{
return FromFile (sFileName, size.Width, size.Height);
}
}
}
--IconFactory.cs: END--
 
Hi,
Well, thanks. Would you please tell me how to do the same thing using the
resource files? I mean what if I store my icon in the resource file?

Does resource files means manifest resource?
Hum.. can Compact Framwork project can includes manifest resource?
 
What's the manifest resource? I meant the ".resx" files. Sorry for being
this dumb, but I'm not a C# (nor a .NET) programmer.
 
Hi,
What's the manifest resource?

Sorry, I used "manifest resource" to mean the .NET resource in main seemlby.
I meant the ".resx" files. Sorry for being

".resx" can contains only string data. and ".resx" is .NET resource, is not
legacy Win32 resource.

In Compact Framework project, I guess (sorry, I had never check it up) that
the legacy Win32 resources (.ico , .bmp, and so on) are put on there
(=application folder) *as is*.
 
Back
Top