tags:

views:

963

answers:

4

hi I have a collection of nice .png files.... meanwhile, I'm developing a win-based software and need some .ico files as icons for toolbar buttons and ....

Is there any way to use .png file as an icon ? or what?

Thank you

A: 

If you're loading the images from a resource file, then no, you can't use PNGs, you have to use ICOs. Fortunately, there are a number of tools that can convert PNGs into ICOs, including ImageMagick (great for automation), and MSPaint as a lowest common denominator.

If you're loading image files at runtime, then you can load any type of image format you want (e.g. use libpng for loading PNGs), but you still have to convert them to icons internally before you can do interesting things with them, such as setting them as a window's icon. Once you've decoded the image data, it's not terribly difficult to convert it to the proper format, but it's not trivial, it just involves a lot of data mangling and strange structs and function calls from the Win32 API.

Adam Rosenfield
+6  A: 

As a workaround you can use IrfanView to convert your *.png file to *.ico file (or any other image to ico) & use it.

http://www.irfanview.com/main_download_engl.htm

Ganesh R.
A: 

If you are using .NET this is not a real problem for you, because afaik PNG support is already build in. You are probably talking about native C/C++ development with GDI/win32? To my knowledge you will not accomplish this by simply using GDI. There are a couple of options where you can set ONE color as transparent then load a simple BMP/JPEG and do some BITMAP tricks however using ICO/GIF will be far easier for this. What you are probably looking for is a working GDI+ example which will use a PNG with alpha channel? This is just an excerpt and I left out the whole mess loading external functions from a DLL part, but maybe this will help you:

static GpImage *background = NULL;

GDIPLOADIMAGEFROMSTREAM GdipLoadImageFromStream;
GDIPLUSSTARTUP          GdiplusStartup; 
GDIPPLUSSHUTDOWN        GdiplusShutdown;
GDIPCREATEFROMHDC       GdipCreateFromHDC;
GDIPDELETEGRAPHICS      GdipDeleteGraphics;
GDIPDRAWIMAGEI          GdipDrawImageI;
GDIPDRAWIMAGERECTI      GdipDrawImageRectI;
GDIPLUS_STARTUP_INPUT   GdiplusStartupInput; 

void LoadPNG(GpImage **image, int resource, HMODULE hInstance)
{
  HRSRC     resrc;
  LPSTREAM  lpstr;
  HGLOBAL   hPng;
  LPVOID    fByte; 
  GpImage *img  = NULL;

  resrc = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(resource), TEXT("PNG"));
  hPng  = LoadResource(GetModuleHandle(NULL), resrc);
  fByte = LockResource(hPng);
  lpstr = SHCreateMemStream(fByte, 200000); 
  GdipLoadImageFromStream(lpstr, &img); 
  *image = img;
}

void CreateBack(HWND hWnd)
{
  HDC  memDC = NULL;      
  HDC  hdc   = NULL; 
  RECT rect;

  DeleteObject(curBack);
  GetClientRect(hWnd, &rect);
  hdc = GetDC(hWnd); 

  memDC   = CreateCompatibleDC(hdc); 
  curBack = CreateCompatibleBitmap(hdc, rect.right, 44);   
  SelectObject(memDC, curBack); 

  /* gdiplus - background*/ {
    int e = 0;
    GpGraphics *g;  
    GdipCreateFromHDC(memDC, &g); 
    GdipDrawImageRectI(g, background,  e, 0, 971, 44);
    GdipDeleteGraphics(g);  
  }

  DeleteObject(memDC);  
  ReleaseDC(hWnd, hdc);  
}

Just a quick note: This GDI+ stuff is really CPU/memory intensive for a couple of reasons. Although fun I did abandoned this approach in favor of gdi and simple BMPs.

merkuro
One thing I left out, to get this working you need to switch to owner drawn buttons in your toolbar... Only worth it in my opinion if you have a changing or multicolored background.
merkuro
A: 

You can simply convert the images to ico files online Icon Convert.

php html