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); }
LVL 6
so3Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
REA_ANDREWConnect With a Mentor Commented:
this wowrks perfectly;

        private void Form1_Load(object sender, EventArgs e)
        {
            WebClient client = new WebClient();
            try
            {
                byte[] icondata = client.DownloadData("http://www.wowwiki.com/favicon.ico");
                using (MemoryStream ms1 = new MemoryStream(icondata))
                {
                    Icon i = new Icon(ms1);
                    this.Icon = i;
                }
            }
            catch (SystemException ex) { Debug.WriteLine(ex.Message); }
        }
0
 
RubenvdLindenCommented:
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.
0
 
REA_ANDREWCommented:
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
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
so3Author Commented:
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.

0
 
REA_ANDREWCommented:
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

0
 
so3Author Commented:
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.
0
 
so3Author Commented:
In fact it seems that your code  doesn't return error on other types. Can you confirm that?
0
 
REA_ANDREWCommented:
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
0
 
REA_ANDREWCommented:
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
0
 
so3Author Commented:
All the files that i download seems to have ico extension maybe this is why i don't get anymore errors:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.