שתף באמצעות


Converting PNG to ICO

Question

Tuesday, April 10, 2012 1:32 PM

I have read the threads on converting PNG to ICO format.  I came up with the code below.  This works, but it creates a black background where the PNG had a transparent background.

            Using _Bmp As New Bitmap(_File)
                Dim _Ms As New MemoryStream
                Icon.FromHandle(_Bmp.GetHicon).Save(_Ms)
                My.Computer.FileSystem.WriteAllBytes(_File.Replace(".png", ".ico"), _Ms.ToArray, False)
            End Using

The _File variable above is a string that has the path to the PNG file.

Any help will be greatly appreciated. 

RDM

All replies (8)

Tuesday, April 10, 2012 3:44 PM ✅Answered

AHA!  I found an article that says this only works on files up to 32 X 32 pixels.  When I used that size, I got transparency correct.  The icon image, however, is not very clear.  I may need to look at opening the 48 X 48 size file and converting it down to 32 X 32 to save it as an icon.  If anyone has any thoughts on that, I would appreciate it.

RDM


Tuesday, April 10, 2012 1:44 PM

Full sample on this official Microsoft page

http://msdn.microsoft.com/en-us/library/system.drawing.icon.fromhandle.aspx

Success
Cor


Tuesday, April 10, 2012 1:46 PM

Thank you for the reply.  The link is NOT a "full sample" as it does not demonstrate how to write the icon to the disk.  I am trying to write a converter to convert a batch of PNG files to ICO files and preserve the transparency.

RDM


Tuesday, April 10, 2012 2:34 PM

Maybe you can take a look at these pages at:

http://support.microsoft.com/kb/94961/en-us

http://www.codeproject.com/Articles/7977/Transparent-Bitmap-Buttons-with-Alpha-Blended-Regi

http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/bbb46ef5-6db2-4ddf-bab1-25b8126b0737

Hope it helps.

My blog: http://soho-hsh.blogspot.com


Tuesday, April 10, 2012 2:49 PM

Thank you for your post.  I am not trying to create a transparent bitmap.  I am not trying to create transparent buttons.  I just want to be able to save a PNG file in an ICO format, maintaining the transparency.  I know there are online tools to do this, but I have a large number of files I want to convert and I just want to create my own converter.  I know this cannot be too hard.  I am just missing some simple code in my example above.  if anyone has any suggestions on how I can do this, please let me know.

RDM


Tuesday, April 10, 2012 3:15 PM | 1 vote

Try this:

   Private Declare Function DestroyIcon Lib "user32.dll" (ByVal handle As IntPtr) As Boolean
      Dim hIcon As IntPtr
      Dim bmp As Bitmap

      Using tmpBmp As New Bitmap("D:\unbenannt.png")
         'tmpBmp.MakeTransparent(Color.FromArgb(247, 247, 247))
         bmp = New Bitmap(tmpBmp, 32, 32)
      End Using

      hIcon = bmp.GetHicon

      Try
         Using ico = Icon.FromHandle(hIcon), _
            fs As New IO.FileStream("d:\test.ico", IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.None)

            ico.Save(fs)
         End Using
      Finally
         DestroyIcon(Handle)
      End Try

Is transparency retained?

Armin


Tuesday, April 10, 2012 3:33 PM

Armin,

I appreciate the reply.  I was just trying that code when I got notice of your post (I found it on another forum).  It does not retain the transparency.  I event tried it with and without the MakeTransparent line (I see yours is commented out).  While this is a trivial need for me, the more I try to do it the more urgently I want to get it right! 

Here is an interesting find:  When I open the ico file in Windows Photo Viewer, it appears to have a tranparent background.  When I view it in Windows Explorer, however, it appears to have a black background.  When I use the icon in my application, it has a black background.  The icon also has relatively poor resolution compared to the original PNG image (both are 48 X 48 pixels).

Frustrated in Houston!

RDM


Tuesday, April 10, 2012 5:01 PM | 1 vote

Hi,

to create icons from bitmaps, some more work is needed...

see:

http://social.msdn.microsoft.com/Forums/en-NZ/csharpgeneral/thread/d317d9e0-11e3-4bff-99e0-d11538936639

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cf335426-731e-4df8-a46c-b83f65a83f7c

to write icons yourself, see:

http://www.vbaccelerator.com/home/NET/Utilities/Icon_Extractor/article.asp

and

http://www.codeproject.com/Articles/16178/IconLib-Icons-Unfolded-MultiIcon-and-Windows-Vista