I want to show image with extension *.ico and I use Stream to do it. But I have a problem that.
With Extension *.jpg, *.bmp... Image show ok but *.ico, it does not show
Here is my code:
 private void OutputStream(string fileName)
    {
        try
        {
            Stream dataStream = null;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPFile spFile = web.GetFile(fileName);
                    dataStream = spFile.OpenBinaryStream();
                    this.HandleOutputStream(dataStream);
                });
        }
        catch (System.Threading.ThreadAbortException exTemp)
        {
            logger.Error("KBStreamPicture::OutputStream", exTemp);
        }
        catch (Exception ex)
        {
            //System.Diagnostics.Debug.Write("OutputStream::" + ex);
            logger.Error("KBStreamPicture::OutputStream", ex);
        }
    }
and
private void HandleOutputStream(Stream dataStream)
    {
        const int bufferSize = 16384; //16KB 
        using (Stream file = dataStream)
        {
            if (file != null)
            {
                this.Page.Response.Clear();
                this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
                this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20)); //only cache 20 minutes
                byte[] buffer = new byte[bufferSize];
                int count = file.Read(buffer, 0, bufferSize);
                while (count > 0)
                {
                    if (this.Page.Response.IsClientConnected)
                    {
                        this.Page.Response.OutputStream.Write(buffer, 0, count);
                        count = file.Read(buffer, 0, bufferSize);
                    }
                    else
                    {
                        count = -1;
                    }
                }
                this.Page.Response.End();
            }
        }
    }
Please help me to resolve that problem.
You're not setting the ContentType property on the Response. Try setting the ContentType to image/x-icon (please note that the "correct" content type may be image/vnd.microsoft.icon, but this post seems to indicate you may run into problems with that type).
The code should look something like:
this.Page.Response.Clear();
this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20));
this.Page.Response.ContentType = "image/x-icon";
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With