Convert .ico to .png

Hello,
i like to get an icon from a homepage convert it to a png file and save it to disk.

            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create("https://icons.duckduckgo.com/ip2/" + domain + ".ico ");
            webrequest.AllowAutoRedirect = true;
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            var content = webresponse.GetResponseStream();

?
?
?
?


            File.WriteAllBytes("somepath", mypngasbyte);

The ??? mark the part where i have no idea how to convert the icon file to a png i can use with unity.

There’s a LOT of responses on Google for unity read ico file

Did you try even one of them?

I did find nothing for unity3d since most converters involve system.drawing.
And yes i dont like really like posting in the unity forum so i post only if i find nothing.

So google shows with the search “Unity3d Convert .ico to .png” only about 12 results to me and only the first 3 results are relevant. On that three results that question was asked but the answer was only “people convert it normally with an external program”
“unity read ico file” gives me far more results but only 2 relevant. And no one answers that.

And if i use google later again with that same question i will have one more result with the answer being “use google”.

Please don’t ask questions that are not related to 2D features on this forum.

I’ll move your post to the Scripting forum. Alternatives are the Graphics forum.

Thanks.

1 Like

If you have to do it in runtime and you can’t depend on system.drawing, I would consider just writing a reader myself, the file format is fairly short and simple. It’s just a set of uncompressed images, with the exception of png compression on the 256x256 full color version.

To convert to and from png, Unity has some built in functions.

Unity does not seem to have System.Drawing included, atleast using System.Drawing does not seem to be available in unity projects. Is there a trick to get it or is it not implemented?

For writing a reader i have not even an idea how to get started i have never done direct image conversions and to research the right code might take a while.

Here it says to just switch to .net 4:

I’ve used it before in Unity 2018.3.0, there scripting runtime is set to .net 4.x indeed, that seems to work fine.

I’ve successfully used this code snippet in the past to create an .ico file from PNGs: A simple class that converts a image to a icon in c# without losing image color data, unlike System.Drawing.Icon; ico with png data requires Windows Vista or above · GitHub

I know this is the opposite of what was originally asked, but it’s not too hard to adapt the code to do the reverse and read the data in an .ico file (without relying on System.Drawing.dll).

2 Likes

Thankyou everyone,

I got it running for now with bitmapfile.Save(ms, System.Drawing.Imaging.ImageFormat.Png); after i copied the System.Drawing.

However i will take an other look at the code snipped as soon i want to convert it to android.

In case anyone comes across this in the future:
I’ve looked into the whole thing a bit more. It seems implementing reading and writing of .ico files yourself is not as straight-forward as I initially thought. If you only care about Windows, definitely use System.Drawing and be done with it.

For anyone that wants to do this on other platforms as well (WebGL, Android, etc.) I’ve released a cross-platform utility for reading and writing .ico files in the Asset Store, which is currently also on sale.