Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2095
  • Last Modified:

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); }
0
so3
Asked:
so3
  • 5
  • 4
1 Solution
 
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
 
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now