This file looks OK to me. I have permissions. See attached icon file (894 bytes long).

Dani AI

Generated

Short answer: the ICO format is a small container (multiple images/sizes) and is not the same as a raw bitmap stream, so constructing a Bitmap directly from an ICO stream often fails. was right to point people at the Icon class; that is the reliable way to convert an ICO (file or stream) into a Bitmap, which also explains the behavior saw when saving the stream to disk then loading it.

Use the Icon constructor that accepts a Stream and call ToBitmap(). Example pattern:

byte[] icoBytes = /* downloaded bytes */;
using (var ms = new System.IO.MemoryStream(icoBytes))
using (var icon = new System.Drawing.Icon(ms))
{
    Bitmap bmp = icon.ToBitmap(); // caller must dispose 'bmp' when finished
    // use bmp (assign to UI, save, etc.)
}

Troubleshooting tips:

  • Make sure the stream actually contains ICO data and is positioned at the start (ms.Position = 0).
  • A quick header check for an ICO file: first four bytes are 0x00,0x00,0x01,0x00.
  • The GDI+ error messages like "OutOfMemory" or "Parameter is not valid" are generic and often mean "unsupported or unrecognized image format," not actual memory exhaustion.
  • If conversion still fails for a specific icon, save the bytes to disk and open in an icon viewer/editor to inspect contents; some ICOs embed different internal image encodings or sizes that can affect decoding.

This approach ties together 's observation (Image/Bitmap won’t always load .ico bytes directly) and ’s Icon-based solution — constructing an Icon from the stream then calling ToBitmap() avoids the Stream->Bitmap failure that started this thread.

Recommended Answers

All 5 Replies

It seems this method doesn't support ico file. I tried with .bmp files, its working fine.

It seems this method doesn't support ico file. I tried with .bmp files, its working fine.

Are you sure this method doesn't support ico files? I haven't seen that claim anywhere until now. Sure would be helpful to know for sure.

Icon class is there.

System.Drawing.Icon c = new Icon(@"c:\app\mapp\p1.ico");
pictureBox1.Image = c.ToBitmap();
commented: thats it! :) +7

Are you sure this method doesn't support ico files? I haven't seen that claim anywhere until now. Sure would be helpful to know for sure.

Even i have not seen such claim, but i tried and was talking about result.

Icon class is there.

System.Drawing.Icon c = new Icon(@"c:\app\mapp\p1.ico");
pictureBox1.Image = c.ToBitmap();

I tried that--it did work. What I can't figure out is how to convert my Stream containing the icon (bitmap). I saved that icon to a file from the stream in order to narrow down the problem.

I don't understand why the Bitmap object initialization works from the file OK in this case, but not from my stream:

MemoryStream ms = new MemoryStream(webClient.DownloadData(uriIcon));
            Bitmap bmp = new Bitmap(ms); // throws exception

But, if I save the memorystream above to a file, then read it with the code you gave, it works (no exception thrown).

This failure (ParameterInvalid exception) only occurs with certain icons I test, which is why I provided the sample. How can I convert this icon directly from my stream to a bitmap without having to save it to a file first?

Thanks,
David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.