I am trying to take favicons and add them to a dynamic image that I am creating. See StackFlair. The website code works fine locally, and on one shared hosting server. blah blah, free hosting plan, you get what you pay for, blah blah My trouble is that I get an exception from a new hosting setup. This exception only happens for .ico files. I can process .gif and .png images just fine on all servers I've tested (ie, a gravatar image). The favicons I am trying to use are favicons from the SE network, but even http://www.google.com/favicon.ico results in the following exception.
System.ArgumentException: Parameter is not valid.
- System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
- System.Drawing.Image.FromStream(Stream stream)
The variations of code that I am trying are below. I get the same Parameter not valid exception for all variations.
byte[] imageBytes = //pull from Image field in SQL Server
//or
byte[] imageBytes = new WebClient().DownloadData(imageUrl);
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);
//or
Icon icon = new Icon(ms);
Image image = icon.ToBitmap();
//or
Image image = new Bitmap(ms);
All of these work just fine locally and on the bad hosting server. None of them work on the server I want to be on. By using Trace output, I can verify that the length of the array contains the correct number of bytes. If I do the following, I see the image displayed as expected.
Response.Clear();
Response.BinaryWrite(imageBytes);
Response.End();
If I loop through the array and write out each byte value, the output is identical from my local instance to the server where I get the exception.
If it helps, the server where my code doesn't work is a Windows 2003 server with sp2.
Clearly the framework is telling me that the stream of bytes is not valid, but everything I've checked, checks out. Any ideas on why this specific server is choking on .ico files?