Opening icons in Visual Studio - RealWorld forums

Log-in or register.

Opening icons in Visual Studio

How to get more than 256 colours
ContactGT
on October 5th 2006

We can create fantastic XP icons, but when we open them in Visual Studio (to build into our application), they appear with black borders and all drop shadows are converted to black. We are using windows XP, so how do we open XP icons in Visual Studio so we can integrate them into our XP application?

Visual Studio help (MSDN) mentions 2, 16 and 256 colours, but we are using a fairly up-to-date version of Visual Studio - surely it can handle XP icons??

Vlasta
on October 5th 2006

As strange as it seems, Visual Studio is having problems with XP icons. I assume, you are trying to use the icon from a .net language using form designer.
The bug description and a not very satisfactory workaround is here: http://support.microsoft.com/kb/822488/en-us
I would recommend to export the icon image as png and load the png into the form designer instead of the icon or use the following code to load icon correctly:

public class IconLoader
{
[DllImport("user32.dll", SetLastError=true)]
static extern int DestroyIcon(IntPtr hIcon);

[DllImport("user32.dll")]
static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);

public struct ICONINFO
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}

static public Bitmap DataToAlphaBitmap(byte[] a_data, int sizeX, int sizeY)
{
using (Icon i = new Icon(new System.IO.MemoryStream(a_data, false), sizeX, sizeY))
{
return IconToAlphaBitmap(i);
}
}
static public Bitmap IconToAlphaBitmap(Icon ico)
{
ICONINFO ii = new ICONINFO();
GetIconInfo(ico.Handle, out ii);
Bitmap bmp = Bitmap.FromHbitmap(ii.hbmColor);
DestroyIcon(ii.hbmColor);
DestroyIcon(ii.hbmMask);

if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32)
return ico.ToBitmap();

BitmapData bmData;
Rectangle bmBounds = new Rectangle(0,0,bmp.Width,bmp.Height);

bmData = bmp.LockBits(bmBounds,ImageLockMode.ReadOnly, bmp.PixelFormat);

Bitmap dstBitmap=new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0);

bool IsAlphaBitmap = false;

for (int y=0; y <= bmData.Height-1; y  )
{
for (int x=0; x <= bmData.Width-1; x  )
{
Color PixelColor = Color.FromArgb(Marshal.ReadInt32(bmData.Scan0(bmData.Stride * y)   (4 * x)));
if (PixelColor.A > 0 & PixelColor.A < 255)
{
IsAlphaBitmap = true;
break;
}
}
if (IsAlphaBitmap) break;
}

bmp.UnlockBits(bmData);

if (IsAlphaBitmap==true)
return new Bitmap(dstBitmap);
else
return new Bitmap(ico.ToBitmap());
}
}
ContactGT
on October 5th 2006

We are not using a form designer in Visual Studio (I think that is Visual Basic). We are using C++, but this problem occurs before we write any code. All we have to do is open an icon in Visual Studio - drag and drop an icon file onto Visual Studio and the background of the XP icon all goes black.

Is there an easy way to convert an XP icon down to a 256 colour icon, but keep any shadows - make them real pixels and part of the palette? When we convert an icon down to 256 colours, the shadow seems to disappear!

Vlasta
on October 5th 2006

In C++ the problem is not so bad. Just do not open the icon with the Visual Studio editor and all will be fine.

I believe the fastest way is to create a new empty icon in Visual Studio, set its identifier and file name, save all changes and finally replace the icon with the real one on disk and compile. APIs like LoadIcon or LoadImage will work fine.

As for the second question - the shadow is just a semitransparent black color, so a short answer is no. When the editor creates a 256-color version from a XP one, it has to convert the semitransparent pixels to either fully visible or completely transparent. The "Create pre-XP formats" command gives you options to optimize the icon for certain backgrounds - you can experiment with the options, but in general you cannot have the same quality on all kinds of background with 256 colors.

ContactGT
on October 5th 2006

Thanks, but even when we manually add icons into the .rc file and don't open them in Visual Studio, we still get icons with a black background and black shadow in the application where the alpha channel is converted to black.

Vlasta
on October 5th 2006

Then maybe there is problem later in the code. If you are using image lists, be sure to use the ILC_COLOR32 flag for example like this:

IMAGELIST hIL = ImageList_Create(16, 16, ILC_MASK|ILC_COLOR32, 4, 4);
ImageList_AddIcon(hIL, LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YOURICON)));

ContactGT
on October 5th 2006

Thanks very much, this fixed the problem perfectly!

Anonymous
on December 22nd 2006

I think there are 2 problems here. first, simply give the ILC_MASK which will fix transparency, but what about the alpha channels? the post starts with that problem, but the answer about not openening doesn't seem to fix the alpha channels in the icons (I tried). With ILC_MASK they are transparent but alpha channels are lost, so you get the ugly edges. The solution of conversion seems overkill. but it is a problem seeing the bug report from MS. however, they don't specify how to solve it with simple c++ without MFC

Page views: 13943       Posts: 8      
Vista & Win 7 icons
I wish there were...
Select background