- Home /
Runtime Image Conversion (C#) .ICO to .PNG? (.ICO not supported; WWW.texture only accepts .PNG and .JPEG)
Any alternatives? Perhaps I should use WWW.Bytes and somehow transform that array of bytes from .ICO to .PNG?
Quote from Unity3D Docs:
The data must be an image in JPG or PNG format. If the data is not a valid image, the generated texture will be a small image of a question mark.
And indeed, that's what I am getting — a red-question-mark photo! I am 100% sure that I am passing the correct link of the .ICO file and successfully downloading it.
Here's the download code:
void Update()
{
if (gotIconURL && !gotIconTexture)
{
//StopCoroutine("GetIcon");
StartCoroutine("GetIcon");
gotIconURL = false; // We don't have the new/future icon URL
}
}
IEnumerator GetIcon()
{
///NOTE!
///Each invocation of texture property allocates a new Texture2D, do something about it!
///If I continously download textures I must use LoadImageIntoTexture or Destroy the previously created texture.
// Start a download of the given URL
var www = new WWW(iconURL); // e.g. https://www.google.com/favicon.ico
//Debug.Log(iconURL);
// Wait for download to complete
yield return www;
// Assign texture
rawIcon.texture = www.texture;
gotIconTexture = true;
}
Any ideas?
The answer to this is spelled out specifically by the error message. If you want to convert from ico then you'll need to find something that can convert it to jpg or png.
@Dave I do realize that. I know what the error is. I merely need some directions on converting an array of bytes representing .ICO image to a PNG one so I could use it as a Texture2D...
This is not a very portable solution, but you could use the Bitmap class from the System.Drawing assembly to read in the icon and then save it out to a memory stream in the format of your choice. If I'm not mistaken, it uses system codecs to handle the file I/O though, so this might be limited to Windows, but you might find a way to include the appropriate libraries with your distro.
Answer by Paulius-Liekis · Jul 16, 2015 at 03:00 PM
Generally people convert it offline, i.e. using some program and then simply download png (instead of ico or other format). If you really need to support ico, then you will have to find a converter and integrate it into your app. There is no simple solution - Unity has no API for that.
Thanks for your response. I hoped somebody would provide a C# solution cuz as far as I know it is possible to do this in C# (e.g. using bitmap decoder).
@TechSupportInco$$anonymous$$g1:
Well, it's of course possible since the ICON format is a relatively simple one and quite well documented. If you were to build a native .NET / C# application you could simply use the wrapper classes that $$anonymous$$icrosoft provides in their libraries. I'm not sure if those are even implemented in the $$anonymous$$ono framework and if they are compatible with Unity. Even if they are you would have to include the assembly that contains the "Icon" class and of course all dependencies (which can result in a huge amount). Also you would be restricted to window as target platform since the .NET / $$anonymous$$ono class "Icon" just wraps the native icon support of windows.
It's probably easier to write your own Icon loader. I was actually very suprised that the wikipedia article is almost like a tec-specification. It should be all you need to know to load a ICO file yourself. It's of course quite a bit of work and since the format has grown in features overtime you probably won't implement all possible cases.
I already thought about writing an ICO and CUR loader myself, but i'm not sure if i can find the time ^^.
@Bunny83: Thank you for the informative answer you provided. Also for the Wikipedia article reference. If I ran out of options and had to port the project to several platforms, then yes I suppose I will have to write my own ICO loader, which I wouldn't $$anonymous$$d to invest some time in. Also, what do you mean with CUR loader?
EDIT: never$$anonymous$$d, mouse cursor :P Graphics file format for mouse cursors.
@Paulius Liekis: I need to perform the conversion in run time. So offline converters won't work in my case.
Since you're grabbing the .ico files from a web link, have you considered converting them on the web server as you serve them up so Unity only sees the .png?. You should be able to convert them in real time on the server. This of course wouldn't be viable if you're not in control of the server that's hosting the files, but thought I'd throw it out there.
It can never be easy right? :) Another thought then - if you can't find a good portable C# implementation, you can maybe do something with custom Unity plugin.
Answer by TechSupportIncoming1 · Jul 17, 2015 at 07:59 AM
Apparently nobody went through this issue I am having. Suppose I have to convert the image to PNG using something like the answer below and then use Resources.LoadAsset...
http://stackoverflow.com/a/10377361
Getting the variable ICON link in real-time was a real headache, let alone converting it now and THEN loading it back into Unity! May the force be with me lol
You can use Resources.LoadAsset only for assets that live in Resources folder only. So I get an impression that you're confused about what's possible. Please read about Resources and AssetBundles, that might give you some ideas how to solve your problem in other ways.
Yeah, you can't use Resources.Load, but once you get the raw PNG bytes and do Texture2D.SetPixels.
Your answer
Follow this Question
Related Questions
How to assign texture from url 1 Answer
WebClient on Android 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using WWW class to get multiple urls 2 Answers