odiseh
odiseh

Reputation: 26547

Using .png files instead of using .ico files in windows programs

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

Upvotes: 9

Views: 33444

Answers (4)

adiian
adiian

Reputation: 1392

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

Upvotes: 3

merkuro
merkuro

Reputation: 6177

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.

Upvotes: 2

Ganesh R.
Ganesh R.

Reputation: 4385

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

Upvotes: 9

Adam Rosenfield
Adam Rosenfield

Reputation: 400642

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.

Upvotes: 0

Related Questions