Link to home
Start Free TrialLog in
Avatar of so3
so3Flag for Romania

asked on

Error loading image

Why i keep receive error "Invalid parameter" on some icons. I can't get the image from the stream.
The RawFormat for this ico is MemoryBmp which i get it from a Imagelist. If i try to read the image from code i get the error.


 WebClient client = new WebClient();
            try
            {
                byte[] icondata = client.DownloadData("http://www.wowwiki.com/favicon.ico");
                Image img = Image.FromStream(new MemoryStream(icondata));
            }
            catch (SystemException ex) { Console.WriteLine(ex.Message); }
Avatar of RubenvdLinden
RubenvdLinden

Icons can contain overhead data. If the image is not at the beginning of the stream, the Image.FromStream method will fail. In this blog, you can find code to create a workaround class for this problem:
http://www.pixvillage.com/blogs/devblog/archive/2005/03/08/151.aspx

Unfortunately, you will need to know the offset of your icon file. Unfortunately, I cannot help you with that.
so3:

Can you confirm that your icon data is not at the start of the stream? Do you know this to be true?  

I would have thought that it is since you are referencing the file itself to download and you assign the received bytes into an array its offset would be zero.

Can you show me the exact error and some more code to see your attempts.

Andrew
Avatar of so3

ASKER

if you launch the code posted in the question you will get the error "Invalid parameter". The icon contains image data because if i save the bytes(or the stream to the file) i can see it without problem in Windows explorer.

The bytes starts with: 0 0 1 0 1 0 16 16
 so it looks liek is icon data saying size 16,16

Please run the code to see exact error.

And as i said if i save the data to a file i can load it in other programs but if i load it in .net i get out of memory with any kind of methods.

lol I know the error.  You the compression is always going to be wrong as you are trying to assign an .ico stream to be an image. An icon is an icon not an image programatically

ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of so3

ASKER

Thanks it worked. so simple:)

One more question is there a way to detect the type from the stream? Because i have a function that get favicons from net and in some cases the image isn't an icon. And i'll still get errors when trying to load them

So is there a way to detect image type or is there a way to use something that automatically makes conversion to image.

If you know i'll give another 500 points.
Avatar of so3

ASKER

In fact it seems that your code  doesn't return error on other types. Can you confirm that?
you have the extension of the file you are trying to download which gives you a lot of info.  

Instantiate an image from an imagestream, Instaniate an icon from an icon stream, etc....
 :-)

Andrew
p.s. unless another question is asked which I do not think relevant here, more points is against the EE usage policy.  But thanks for the offer.

:-)

Andrw
Avatar of so3

ASKER

All the files that i download seems to have ico extension maybe this is why i don't get anymore errors:)